Many of our customers have asked if C1TrueDbGrid offers conditionally enabling/ disabling a cell focus. This blog explains an approach to do the same. That is to allow or disallow focus when user navigates the grid either with the Arrow keys, the TAB key, or clicking with the mouse. The process used for this approach is : 1. Bind C1TrueDbgrid with a DataSouce having a boolean column. The boolean column will be displayed as a checkbox. 2. Conditionally allow focus in a column based on the CheckedState of the boolean column in BeforeRowColChange event.
For simplicity we are binding C1TrueDbGrid with a DataTable with a boolean 'Allow Focus' column that will be displayed as a checkbox.
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim table As New DataTable
With table.Columns
.Add(New DataColumn("A", GetType(Boolean)))
.Add(New DataColumn("B", GetType(String)))
.Add(New DataColumn("C", GetType(String)))
.Add(New DataColumn("D", GetType(String)))
End With
C1TrueDBGrid1.SetDataBinding(table, "", True)
C1TrueDBGrid1.Row = 0
C1TrueDBGrid1.Col = 0
C1TrueDBGrid1.Focus()
End Sub
The CheckedState of the 'Allow Focus' column will determine whether the 'If Checked' column will be allowed focus or not as follows :
This will be handled in the BeforeRowColChange event which occurs before moving onto the another cell.
Private Sub C1TrueDBGrid1_BeforeRowColChange(ByVal sender As Object, ByVal e As C1.Win.C1TrueDBGrid.CancelEventArgs) Handles C1TrueDBGrid1.BeforeRowColChange
' Fetches current row index
Dim curRow As Integer = C1TrueDBGrid1.Row
' Fetches destination row index
Dim desRow As Integer = C1TrueDBGrid1.DestinationRow
' Fetches current column index
Dim curCol As Integer = C1TrueDBGrid1.Col
' Fetches destination column index
Dim desCol As Integer = C1TrueDBGrid1.DestinationCol
' Checks before moving into Column(2) if the value in Column(0) is true/false
If desCol = 2 Then
If C1TrueDBGrid1.Columns(0).CellValue(desRow).ToString() = "False" Then
e.Cancel = True
If curRow = desRow Then
If curCol < desCol Then
C1TrueDBGrid1.Col = desCol + 1
Else
C1TrueDBGrid1.Col = desCol - 1
End If
Else
If curRow < desRow Then
C1TrueDBGrid1.Row = desRow + 1
Else
C1TrueDBGrid1.Row = desRow - 1
End If
End If
End If
End If
End Sub
Download the sample for detailed implementation of the above code. Download Sample