VSFlexGrid Control > VSFlexGrid Properties, Events, and Methods > VSFlexGrid Properties > ColIndex Property |
Returns the column index that matches the given key.
val& = [form!]VSFlexGrid.ColIndex(Key As String)
The ColIndex property is used in conjunction with the ColKey property to identify and refer to columns regardless of their physical position on the grid. These properties are useful when the grid is bound to a recordset or when the user is allowed to move columns around using the ExplorerBar.
To use these properties, assign unique keys to each column using the ColKey property. When you want to refer to a specific column, convert the key into an index using the ColIndex property.
For example, the code below counts how many times the MouseMove event fired and displays the total on a "Counter" column. The user may move the column to a different position with the mouse, and the code will follow the column around:
Private Sub fg_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim c%
c = fg.ColIndex("Counter")
fg.TextMatrix(1, c) = fg.ValueMatrix(1, c) + 1
End Sub
Private Sub Form_Load()
fg.ExplorerBar = flexExMove
fg.TextMatrix(0, 1) = "Counter"
fg.ColKey(1) = "Counter"
End Sub
When the grid is bound to a recordset, ColKey values are automatically set to the field names. This allows you to refer to columns by their field names, as in the following code (which assumes the fg grid is bound to a recordset with a field called "CategoryID"):
fg.ColAlignment(fg.ColIndex("CategoryID")) = flexAlignCenterCenter
If the Key argument does not correspond to any ColKey value, the ColIndex property returns -1.
Long