Storage Mode > Interactions between True DBGrid and XArrayDB > Updating XArrayDB elements |
With the XArrayDB object, you always have instant access to all "records" in your "database," so you can update cells in different rows directly without having to move the current record pointer, initiate edit mode, modify one or more cells, then update the changed record. For this reason, it is usually more convenient to make changes to XArrayDB directly, then use the grid's Refresh method to update the display.
However, if you have modified only a single row or column, it is not necessary to refresh the entire grid; you can use the RefetchRow or RefetchCol methods to update only the modified cells within the grid's display. Likewise, if you have modified only a single cell, you can use the RefetchCell method of the corresponding Column object.
Note: Take care to use the correct method when updating a row, column, or cell. The Refetch*** methods force the grid to request cell data from the associated XArrayDB object. The Refresh*** methods repaint the affected areas of the display using (unmodified) data from the grid's cache.
It is important to note that when you change one or more elements in an XArrayDB object, you must refresh either the affected cells or the entire grid, otherwise the display will not reflect the correct data. Consider the following example, which implements a command button that clears the contents of the current grid row, then sets focus to the grid:
Example Title |
Copy Code
|
---|---|
Private Sub Command1_Click()
Dim row As Long, col As Integer
With TDBGrid1
row = .Bookmark
With MyArray
For col = .LowerBound(2) To .UpperBound(2)
.Value(row, col) = ""
Next col
End With
.RefetchRow
.SetFocus
End With
End Sub
|
Note that the Bookmark property of the grid is used as a row index for the XArrayDB object. The loop that clears the current row also uses the LowerBound and UpperBound properties to iterate over the columns (second dimension) of the array. This technique will work with any XArrayDB object, although you can substitute integer constants if the array bounds are known in advance.
After the array elements are cleared, the grid's RefetchRow method is invoked, causing all unmodified cells in the current row to be repainted as empty. Since no arguments are passed to the RefetchRow method, the grid requests data for the current row.