ComponentOne True DBGrid for WinForms
Restricting Cell Navigation
Run-Time Interaction > Navigation and Scrolling > Restricting Cell Navigation

The BeforeRowColChange event can be used to prevent the user from moving to a different cell, regardless of whether the current cell is modified. Set the Cancel argument to True to keep another cell from becoming current.

If the current cell has been modified, use the BeforeColUpdate event to examine its value before moving to another grid cell. If the value entered is invalid, set the Cancel argument to True to prevent the current cell from changing, and optionally beep or display an error message for the user. The BeforeColUpdate event provides a flexible way to validate user input and restrict cell navigation.

To write code in Visual Basic

Visual Basic
Copy Code
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

To write code in C#

C#
Copy Code
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;
    }
}
See Also