ComponentOne True DBGrid for .NET (2.0) Search HelpCentral 

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 that 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. The following code would be put in the grid's AfterColUpdate event to cause either column to change the Balance column when updated:

·      Visual Basic

Private Sub C1TrueDBGrid1_AfterColUpdate(ByVal sender As Object, ByVal e As C1.Win.C1TrueDBGrid.ColEventArgs) Handles C1TrueDBGrid1.AfterColUpdate

    Dim row as Integer = Me.C1TrueDBGrid1.Row

    Me.C1TrueDBGrid1(row, "Balance") = -e.Column.DataColumn.Value

End Sub

·      C#

private void C1TrueDBGrid1_AfterColUpdate( object sender, C1.Win.C1TrueDBGrid.ColEventArgs e)

{

    int row = this.C1TrueDBGrid1.Row;

    this.c1TrueDBGrid1[row, "Balance"] = -e.Column.DataColumn.Value;

}

·      Delphi

procedure TWinForm.C1TrueDBGrid1_AfterColUpdate(sender: System.Object; e: C1.Win.C1TrueDBGrid.ColEventArgs);

var

  Cols: Columns;

  S: string;

begin

  Cols := Self.C1TrueDBGrid1.C1DataColumnCollection;

  S := Cols[e.ColIndex].Caption;

  if (S = 'Debit') then

    Cols['Balance'].Value = -Cols[e.ColIndex].Value

  else if (S = 'Credit') then

    Cols['Balance'].Value = Cols[e.ColIndex].Value;

end;

Notice that, when updating these columns, the code actually changes the value of the Balance column, which is both bound and invisible.


Send comments about this topic to ComponentOne.
Copyright © ComponentOne LLC. All rights reserved.