ComponentOne True DBGrid Pro 8
Displaying both text and pictures in a cell

Once you have configured the ValueItems collection to perform text-to-picture translations for a column, you can cause both the Value string and the DisplayValue bitmap to appear within the same cell by selecting the AnnotatePicture check box on the Values property page. Or, in code:

Example Title
Copy Code
With TDBGrid1.Columns("Country").ValueItems .AnnotatePicture = True End
    With

The horizontal placement of the bitmap with respect to the cell text is determined by the Alignment and ForegroundPicturePosition properties of the column's Style object. In the following example, Alignment is set to 3 - General. Since the Country column represents a string field, the cell text is left-aligned. However, since the ForegroundPicturePosition property is set to the default value of 0 - Left, the bitmap is placed at the left edge of the cell, and the cell text is left-aligned in the remaining space.

However, if you change the ForegroundPicturePosition property to 1 - Right, then the cell text is left-aligned as usual, but the bitmap is right-aligned.

To place the cell text below the bitmap while centering both items, set the Alignment property to 2 - Center and the ForegroundPicturePosition property to 4 - Top of Text.

Note: For an illustration of all possible combinations of the Alignment and ForegroundPicturePosition properties, see Displaying foreground pictures.

When editing, the editor uses all space available in the text portion of the cell. When the Presentation property of the ValueItems collection is set to one of the combo box options, the bitmap will not change until editing is completed.

Note that in the preceding examples, the text is displayed as it is stored in the database without formatting. But what if you want to display both a picture and formatted text? Since the ValueItem object can only accommodate one translation, you cannot accomplish this with ValueItems alone. However, you can use the FormatText event to translate the text, then use the ValueItems collection to associate the translated text (not the underlying data value) with a picture:

Example Title
Copy Code
TDBGrid1.Columns("Country").NumberFormat = "FormatText Event"

In this example, the NumberFormat property is set to a special value that causes the FormatText event to fire:

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

        Value As Variant, Bookmark As Variant)

 

    Select Case Value

        Case "CAN"

            Value = "Canada"

        Case "UK"

            Value = "United Kingdom"

        Case "USA"

            Value = "United States"

        Case "JPN"

            Value = "Japan"

        Case "AUS"

            Value = "Australia"

    End Select

End Sub

Since the FormatText event now translates the country codes stored in the database into actual names for display, the Value property of each member of the ValueItems collection must be changed accordingly. The following figure depicts the updated Values property page (only the entries in the Value column are different).

Assuming that the Alignment and ForegroundPicturePosition properties are set as in the previous example, the end result is that the underlying data is displayed as both descriptive text and a picture.

Note: DisplayValue pictures are ideally suited for translating status codes or other fields where the number of allowable values is relatively small. If you need a more generalized picture display mechanism than the ValueItems collection offers, you can use the ForegroundPicture property in conjunction with the FetchCellStyle event to display arbitrary pictures on a per-cell basis. For more information, see Displaying foreground pictures.

 

 


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

Product Support Forum  |  Documentation Feedback