How to cancel the current cell focus for empty cells in WinForms GridControl ?
Avoid the selection of empty cells
To avoid the selection of empty cells and not return the coordinates of the empty cells, you can use the following code in the CellClick and CurrentCellActivating events.
void gridControl1_CurrentCellActivating(object sender, GridCurrentCellActivatingEventArgs e)
{
//Avoids the cell click while cells have no value or for empty cells.
GridStyleInfo style = this.gridControl1.GetViewStyleInfo(e.RowIndex, e.ColIndex);
if (e.RowIndex > this.gridControl1.Rows.HeaderCount && e.ColIndex > this.gridControl1.Cols.HeaderCount &&
(style.CellValue == null || style.CellValue == string.Empty))
{
e.Cancel = true;
}
}
void gridControl1_CellClick(object sender, GridCellClickEventArgs e)
{
//Gets the label name of the columns and rows.
string col = GridRangeInfo.GetAlphaLabel(e.ColIndex);
string row = GridRangeInfo.GetNumericLabel(e.RowIndex);
string enablerCellValue = this.gridControl1[e.RowIndex, e.ColIndex].CellValue.ToString();
//Avoids the coordinates of the empty cell.
if (enablerCellValue != string.Empty)
{
MessageBox.Show("The " + col + row + " Cell is clicked");
}
}Private Sub gridControl1_CurrentCellActivating(ByVal sender As Object, ByVal e As GridCurrentCellActivatingEventArgs)
'Avoids the cell click while cells have no value or for empty cells.
Dim style As GridStyleInfo = Me.gridControl1.GetViewStyleInfo(e.RowIndex, e.ColIndex)
If e.RowIndex > Me.gridControl1.Rows.HeaderCount AndAlso e.ColIndex > Me.gridControl1.Cols.HeaderCount AndAlso (style.CellValue Is Nothing OrElse style.CellValue = String.Empty) Then
e.Cancel = True
End If
End Sub
Private Sub gridControl1_CellClick(ByVal sender As Object, ByVal e As GridCellClickEventArgs)
'Gets the label name of the columns and rows.
Dim col As String = GridRangeInfo.GetAlphaLabel(e.ColIndex)
Dim row As String = GridRangeInfo.GetNumericLabel(e.RowIndex)
Dim enablerCellValue As String = Me.gridControl1(e.RowIndex, e.ColIndex).CellValue.ToString()
'Avoids the coordinates of the empty cell.
If enablerCellValue <> String.Empty Then
MessageBox.Show("The " & col & row & " Cell is clicked")
End If
End SubSamples:
C#: CellValues_c#
VB: CellValues_vb
Conclusion
I hope you enjoyed learning about how to cancel the current cell focus for empty cells in WinForms GridControl.
You can refer to our WinForms GridControl feature tour page to know about its other groundbreaking feature representations and WinForms GridControl documentation, and how to quickly get started for configuration specifications. You can also explore our WinForms GridControl example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!