ComponentOne True DBGrid Pro 8
Tutorial 21 - Displaying Arbitrary Bitmaps

In this tutorial, you will learn how to use Style objects to display arbitrary bitmaps within grid cells. In addition to font, color, and alignment settings, Style objects support both background and foreground pictures. Background pictures can be centered, tiled, or stretched to fill the entire cell. Foreground pictures can be positioned relative to the cell text, and can also be displayed using a transparent color.

  1. Start a new project.

  2. Place a True DBGrid control (TDBGrid1), a command button (Command1), and a TextBox control (Text1) on the form (Form1)

  3. Set the DataMode property of TDBGrid1 to 4 - Storage (the default value of this property is 0 - Bound).

  4. Set the AllowUpdate property to False.

  5. Right-click TDBGrid1 to display its context menu and choose the Edit option to enter the grid's visual editing mode. Place the mouse cursor over the row divider, and increase the row size to about double the current height.

  6. Right-click TDBGrid1 again to display its context menu and this time choose Properties to display the Property Pages dialog.

  7. Select the Columns property page by clicking the Columns tab. Set the Caption property of Columns(00) and Columns(01) to "Picture" and "File Name", respectively.

  8. Select the Splits tab and expand the Splits(00) object. Set the ExtendRightColumn property to True.

  9. Set the Caption property of Command1 to "Display".

    Adding XArrayDB to the project

    As in Tutorial 19, we will use the XArrayDB object, therefore we need to add its reference to the project.

  10. Select References from the Project menu to display a list of available type library references. Select the check box labeled ComponentOne XArrayDB Object (XADB8.OCX), then press the OK button.

  11. Add the following declaration to the General section of Form1:

    Example Title
    Copy Code
    Dim x As New XArrayDB
    
  12. Enter the following code in the Form_Load event:

    Example Title
    Copy Code
    Private Sub Form_Load()
    
        ' Initially we resize the XArrayDB to 0 rows and 2 columns.
    
        x.ReDim 0, -1, 0, 1
    
        Set TDBGrid1.Array = x
    
       
    
        ' Enable FetchCellStyle Event.
    
        TDBGrid1.Columns(0).FetchStyle = dbgFetchCellStyleColumn
    
        TDBGrid1.MarqueeStyle = dbgDottedRowBorder
    
    End Sub
    
  13. Add the following code to the Click event of Command1:

    Example Title
    Copy Code
    Private Sub Command1_Click()
    
        Dim s As String
    
        Dim i As Integer
    
       
    
        x.ReDim 0, -1, 0, 1
    
      
    
        i = 0
    
       
    
        ' Fill the XArrayDB object with all bitmap file names found in this
    
        ' directory.
    
        s = Dir(Text1.Text & "\*.bmp")
    
       
    
        While s <> ""
    
            If s <> "." And s <> ".." Then
    
                x.ReDim 0, x.UpperBound(1) + 1, 0, 1
    
                x(i, 1) = s
    
                i = i + 1
    
            End If
    
            s = Dir
    
        Wend
    
       
    
        ' Reinitialize the grid.
    
        TDBGrid1.Bookmark = Null
    
        TDBGrid1.ReBind
    
    End Sub
    
  14. Enter the following code in the FetchCellStyle event:

    Example Title
    Copy Code
    Private Sub TDBGrid1_FetchCellStyle( _
    
        ByVal Condition As Integer, ByVal Split As Integer, _
    
        Bookmark As Variant, ByVal Col As Integer, _
    
        ByVal CellStyle As TrueDBGrid80.StyleDisp)
    
     
    
        Dim PathFileName As String
    
     
    
        ' Full Path file name of a picture.
    
        PathFileName = Text1.Text & "\" & _
    
                       TDBGrid1.Columns(1).CellText(Bookmark)
    
     
    
        ' Load the picture
    
        CellStyle.BackgroundPicture = LoadPicture(PathFileName)
    
    End Sub
    

Run the program and observe the following:

For more information, see Applying Pictures to Grid Elements.

This concludes Tutorial 21.

 

 


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

Product Support Forum  |  Documentation Feedback