Bound Mode > Unbound Columns > Editing unbound columns |
Another technique for updating an unbound column is to use the AfterColUpdate event to adjust the value of other (bound) columns. For example, imagine a pair of columns for Debit and Credit, as shown in this portion of a grid display:
Assume that there is no database field for these, but that they are unbound columns which derive their value from a single Balance column, which is either positive or negative. From the user's perspective, it would be desirable to edit these values directly; from your perspective, it would be desirable to have the grid update the dependent Balance column automatically.
True DBGrid makes such tasks easy. Here's the code you would put in the grid's AfterColUpdate event to cause either column to change the Balance column when updated:
Example Title |
Copy Code
|
---|---|
Private Sub TDBGrid1_AfterColUpdate(ByVal ColIndex As Integer) Dim Cols As Columns Set Cols = TDBGrid1.Columns Select Case Cols(ColIndex).Caption Case "Debit" Cols("Balance").Value = -Cols(ColIndex).Value Case "Credit" Cols("Balance").Value = Cols(ColIndex).Value End Select End Sub |
Notice that, when updating these columns, the code actually changes the value of the Balance column, which is both bound and invisible.