ComponentOne True DBGrid for WinForms
Formatting with a Custom Event Handler
Data Presentation Techniques > Text Formatting > Formatting with a Custom Event Handler

On occasion, you may find that your current formatting options do not suit your particular needs. Furthermore, you may be restricted in the type of formatting that you can use or need a custom formatting option. In these cases, the FormatText Event option can be specified for the NumberFormat property. Choosing this option for a column will cause the FormatText event to fire each time data is about to be displayed in that column. The event allows you to reformat, translate, indent, or do anything you want to the data just prior to display:

To write code in Visual Basic

Visual Basic
Copy Code
Private Sub C1TrueDBGrid1_FormatText(ByVal sender As Object, ByVal e As C1.Win.C1TrueDBGrid.FormatTextArgs) Handles C1TrueDBGrid1.FormatText
 
End Sub

To write code in C#

C#
Copy Code
private void C1TrueDBGrid1_FormatText(object sender, C1.Win.C1TrueDBGrid.FormatTextArgs e)
{
 
}

A member of the FormatTextEventArgs object, ColIndex is the column number of the grid to be reformatted. While the Valuemember contains the current value of the data and also serves as a placeholder for the formatted display value. For example, suppose the first column contains numeric values from 1 to 30, and you wish to display the data as Roman numerals:

To write code in Visual Basic

Visual Basic
Copy Code
Private Sub C1TrueDBGrid1_FormatText(ByVal sender As Object, ByVal e As C1.Win.C1TrueDBGrid.FormatTextEventArgs) Handles C1TrueDBGrid1.FormatText
 
    Dim result As String
 
    If e.ColIndex = 0 Then
 
        ' Determine how many X's.
        While e.Value >= 10
            result = result & "X"
            e.Value = e.Value - 10
        End While
 
        ' Append "digits" 1-9.
        Select Case e.Value
            Case 1
                result = result & "I"
            Case 2
                result = result & "II"
            Case 3
                result = result & "III"
            Case 4
                result = result & "IV"
            Case 5
                result = result & "V"
            Case 6
                result = result & "VI"
            Case 7
                result = result & "VII"
            Case 8
                result = result & "VIII"
            Case 9
                result = result & "IX"
        End Select
 
        ' Change the actual format.
        e.Value = result
    End If
End Sub

To write code in C#

C#
Copy Code
private void C1TrueDBGrid1_FormatText(object sender, C1.Win.C1TrueDBGrid.FormatTextEventArgs e)
 
    string result;
 
    if ( e.ColIndex = 0 ) 
    {
        // Determine how many X's.
        while ( e.Value >= 10 )
        {
            result = result + "X";
            e.Value = e.Value - 10;
        }
 
        // Append "digits" 1-9.
        switch (e.Value) 
        {
            case 1;
                result = result + "I";
            case 2;
                result = result + "II";
            case 3;
                result = result + "III";
            case 4;
                result = result + "IV";
            case 5;
                result = result + "V";
            case 6;
                result = result + "VI";
            case 7;
                result = result + "VII";
            case 8;
                result = result + "VIII";
            case 9;
                result = result + "IX";
        }
 
        // Change the actual format.
        e.Value = result;
    }
}

Since the FormatText event has fewer restrictions than other formatting techniques, you can always use it to gain full control over the textual content of any value displayed in the grid.

See Also