Unbound Mode > Unbound Mode Events > Handling the UnboundWriteData event in modes 1 and 2 |
This event applies to both DataMode 1 - Unbound and 2 - Unbound Extended.
If the AllowUpdate property of the grid is True, and the user has edited data in one or more cells of the current row, then moving to another row will trigger an update. The grid will then fire the UnboundWriteData event, which allows you to use the changed values, passed via a RowBuffer object, to update the data you are responsible for storing and maintaining. The syntax of this event is as follows:
Private Sub TDBGrid1_UnboundWriteData( _
ByVal RowBuf As RowBuffer, WriteLocation As Variant)
When this event is fired, the properties of the RowBuf argument are set as follows:
RowCount is 1, since only one row can be updated at a time.
ColumnCount specifies the number of columns of data in the Value array. This will always reflect the total number of columns in the grid's Columns collection and not just those that are visible on the screen. This ensures that data in invisible columns, which may have been modified by other event handlers such as AfterColUpdate, are also updated to the underlying data source.
The ColumnName array contains the names of the grid columns corresponding to the columns in RowBuf.
The Bookmark array is not used, since WriteLocation specifies the row being updated.
The Value array contains a single row of data. Entries which are Null have not been changed. Entries which are not Null reflect the user's changes.
WriteLocation is a bookmark that specifies the exact row that needs to be updated. This differs from the StartLocation argument in the UnboundReadData event, which specifies the row before or after the desired data, depending on the value of the ReadPriorRows argument.
The following code sample demonstrates how to determine which cells were modified by the user:
Example Title |
Copy Code
|
---|---|
For ColIndex = 0 To RowBuf.ColumnCount - 1 If IsNull(RowBuf.Value(0, ColIndex)) Then ' Cell not modified by the user. Else ' RowBuf.Value(0, ColIndex) contains updated value. End If Next ColIndex |
The RowCount property is always set to 1 for this event, but you should change it to 0 if the update cannot be completed, such as when the underlying database reports an error. The contents of the current cell will be maintained if RowCount is set to zero.
Note: You can force the UnboundWriteData event to occur in code with the grid's Update method. This technique is particularly valuable when the unbound dataset contains a single row and AllowAddNew is False, since there is no way for the user to trigger the update by moving to another row in this case.