)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to cancel the current cell focus for empty cells in WinForms GridControl?

Platform: WinForms |
Control: GridControl |
Published Date: September 2, 2015 |
Last Revised Date: December 20, 2019

Avoid the selection of empty cells

To avoid the selection of empty cells and to not return the coordinates of the empty cells, you can use the following code in the CellClick and CurrentCellActivating events.

 

C#

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");
    }
}

 

VB

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 Sub

 

Samples:

C#: CellValues_c#

VB: CellValues_vb

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon