ComponentOne True DBGrid Pro 8
Formatting with a custom event handler

On occasion, you may find that the Visual Basic formatting options do not suit your particular needs. Or, you may be using True DBGrid in a development environment that does not support Visual Basic formatting, such as Visual C++. 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:

Example Title
Copy Code
Private Sub TDBGrid1_FormatText(ByVal ColIndex As Integer, _

        Value As Variant, Bookmark As Variant)

ColIndex contains the column number of the grid to be reformatted. Value 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:

Example Title
Copy Code
Private Sub TDBGrid1_FormatText(ByVal ColIndex As Integer, _

        Value As Variant, Bookmark As Variant)

 

    Dim result As String

 

    If ColIndex = 0 Then

        ' Determine how many X's.

        While Value >= 10

            result = result & "X"

            Value = Value - 10

        Wend

 

        ' Append "digits" 1-9.

        Select Case 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.

        Value = result

    End If

End Sub

Since the FormatText event is not restricted to a particular development environment, you can always use it to gain full control over the textual content of any value displayed in the grid.

 

 


Copyright (c) GrapeCity, inc. All rights reserved.

Product Support Forum  |  Documentation Feedback