There are many methods available to populate a VSFlexGrid control. Often, you will simply connect it to a database using the DataSource property. Or you could load the data from a file using the LoadGrid method. Finally, you may use the AddItem method to add rows or the Cell property to assign data to cells.
In this demo, we will generate some random data and assign it to the control using the Cell property. This is done at the Form_Load event:
Private Sub Form_Load()
' initialize the control
fg.Cols = 4
fg.FixedCols = 0
fg.GridLinesFixed = flexGridExplorer
fg.AllowUserResizing = flexResizeBoth
fg.ExplorerBar = flexExMove
' define some sample data
Const slProduct = "Product|Flutes|Saxophones|Drums|" & _
"Guitars|Trombones|Keyboards|Microphones"
Const slAssociate = "Associate|John|Paul|Mike|Paula|Sylvia|Donna"
Const slRegion = "Region|North|South|East|West"
Const slSales = "Sales|14323|2532|45342|43432|75877|4232|4543"
' populate the control with the data
FillColumn fg, 0, slProduct
FillColumn fg, 1, slAssociate
FillColumn fg, 2, slRegion
FillColumn fg, 3, slSales
fg.ColFormat(3) = "#,###"
End Sub
This routine uses a helper function called FillColumn that fills an entire column with data drawn randomly from a list. This is a handy function for demos, and here is the code:
Sub FillColumn(fg As VSFlexGrid, ByVal c As Long, ByVal s As String)
Dim r As Long, i As Long, cnt As Long
ReDim lst(0) As String
' build list of data values
cnt = 0
i = InStr(s, "|")
While i > 0
lst(cnt) = Left(s, i - 1)
s = Mid(s, i + 1)
cnt = cnt + 1
ReDim Preserve lst(cnt) As String
i = InStr(s, "|")
Wend
lst(cnt) = s
' set values by randomly picking from the list
fg.Cell(flexcpText, 0, c) = lst(0)
For r = fg.FixedRows To fa.Rows - 1
i = (Rnd() * 1000) Mod cnt + 1
fg.Cell(flexcpText, r, c) = lst(i)
Next
' do an autosize on the column we just filled
fg.AutoSize c, , , 300
End Sub
This concludes the first step. Press F5 to run the project, and you will see a grid loaded with data. Because the ExplorerBar property is set to flexExMove, you may drag column headings around to reorder the columns.
The data presented is almost useless, however, because it is not presented in an organized way. We will fix that next.