ComponentOne True DBGrid for .NET (2.0) Search HelpCentral 

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:

·      Visual Basic

Private Sub C1TrueDBGrid1_FormatText(ByVal sender As Object, ByVal e As C1.Win.C1TrueDBGrid.FormatTextArgs) Handles C1TrueDBGrid1.FormatText

 

End Sub

·      C#

private void C1TrueDBGrid1_FormatText(object sender, C1.Win.C1TrueDBGrid.FormatTextArgs e)

{

 

}

·      Delphi

procedure C1TrueDBGrid1_FormatText(sender: System.Object; e: C1.Win.C1TrueDBGrid.FormatTextArgs);

begin

 

end;

A member of the FormatTextEventArgs object, ColIndex is the column number of the grid to be reformatted. While the Value member 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:

·      Visual Basic

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

·      C#

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;

    }

}

·      Delphi

procedure TWinForm.C1TrueDBGrid1_FormatText(sender: System.Object; e: C1.Win.C1TrueDBGrid.FormatTextEventArgs);

var

  res: string;

  val: Integer;

begin

  if e.ColIndex = 0 then

  begin

    res := ‘’;

    val := Int32.Parse(e.Value);

 

    // Determine how many X's.

    while e.Value >= 10 do

    begin

      res := res + 'X';

      val := val - 10;

    end;

    // Append "digits" 1-9.

    else if (val = 1) then

      res := res + 'I';

    else if (val = 2) then

      res := res + 'II';

    else if (val = 3) then

      res := res + 'III';

    else if (val = 4) then

      res := res + 'IV';

    else if (val = 5) then

      res := res + 'V';

    else if (val = 6) then

      res := res + 'VI';

    else if (val = 7) then

      res := res + 'VII';

    else if (val = 8) then

      res := res + 'VIII';

    else if (val = 9) then

      res := res + 'IX';

 

    // Change the actual format.

    e.Value := res;

  end;

end;

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.


Send comments about this topic to ComponentOne.
Copyright © ComponentOne LLC. All rights reserved.