ComponentOne True DBGrid for .NET (2.0) Search HelpCentral 

C1TrueDBGrid.BeforeColUpdate Event

Occurs before a cell is updated to the datasource.

[Visual Basic]

Public Event BeforeColUpdate As BeforeColUpdateEventHandler

[C#]

public event BeforeColUpdateEventHandler BeforeColUpdate

[Delphi]

public property BeforeColUpdate: BeforeColUpdateEventHandler read remove_BeforeColUpdate write add_BeforeColUpdate;

Remarks

The data specified by the BeforeColUpdateEventArgs.OldValue property moves from the cell to the grid's copy buffer when the user completes editing within a cell, as when tabbing to another column in the same row, pressing the ENTER key, or clicking on another cell. Before the data has been moved from the cell into the grid's copy buffer, the BeforeColUpdate event is triggered. This event gives the application an opportunity to check the individual grid cells before they are committed to the grid's copy buffer.

If your event procedure sets the BeforeColUpdateEventArgs.Cancel property to True, the grid retains focus, and the AfterColUpdate event is not triggered.

Setting the BeforeColUpdateEventArgs.Cancel property to True prevents the user from moving focus away from the control until the application determines that the data can be safely moved back to the grid's copy buffer.

Example

If the value entered is invalid, set the BeforeColUpdateEventArgs.Cancel property to True to prevent the current cell from changing, and optionally beep or display an error message for the user. The following code uses the BeforeColUpdate event to validate user input and restrict cell navigation:

·      Visual Basic

       Private Sub C1TrueDBGrid1_BeforeColUpdate(ByVal sender As Object, ByVal e As C1.Win.C1TrueDBGrid.BeforeColUpdateEventArgs) Handles C1TrueDBGrid1.BeforeColUpdate

           Dim CharCode As Integer

           If e.ColIndex = 1 Then

               ' Data in Column 1 must start with upper case.

               CharCode = Asc(Me.C1TrueDBGrid1.Columns(1).Text)

               If CharCode > 64 And CharCode < 91 Then

                   Exit Sub

               ' Display warning message for user.

               MessageBox.Show("Last name must start with upper case")

               ' Data validation fails, prohibit user from moving to another cell.

               e.Cancel = True

           End If

       End Sub

·      C#

       private void C1TrueDBGrid1_BeforeColUpdate(object sender,  C1.Win.C1TrueDBGrid.BeforeColUpdateEventArgs e)

       {

           int CharCode;

           if ( e.ColIndex == 1 )

           {

               // Data in Column 1 must start with upper case.

               CharCode = this.C1TrueDBGrid1.Columns[1].Text[0];

               if ( CharCode > 64 & CharCode < 91 )

               {

                   return;

               }

               // Display warning message for user.

               MessagBox.Show("Last name must start with upper case");

               // Data validation fails, prohibit user from moving to  another cell.

               e.Cancel = true;

           }

       }

·      Delphi

       procedure TWinForm.C1TrueDBGrid1_BeforeColUpdate(sender: System.Object; e: C1.Win.C1TrueDBGrid.BeforeColUpdateEventArgs);

       var CharCode: Integer;

       begin

         if (e.ColIndex = 1) then

         begin

           // Data in Column 1 must start with upper case.

           CharCode := Integer(this.C1TrueDBGrid1.Columns[1].Text[0]);

           if ( CharCode > 64 & CharCode < 91 ) then

             Exit;       

           // Display warning message for user.

           MessageBox.Show('Last name must start with upper case');

           // Data validation fails, prohibit user from moving to another cell.

           e.Cancel = true;

         end;

       end;

For more details on the BeforeColUpdate event, see the Restricting Cell Navigation topic.

See Also

C1TrueDBGrid Class | C1TrueDBGrid Members | C1.Win.C1TrueDBGrid Namespace


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