Articles in this section
Category / Section

How to select the row while clicking on checkbox cell in WinForms GridGroupingControl?

1 min read

Row selection

To select the row while clicking on the checkbox, the TableControlCheckBoxClick event can be handled. The SetSelected method can be used to select or deselect the row.

this.gridGroupingControl1.TableControlCheckBoxClick += new GridTableControlCellClickEventHandler(gridGroupingControl1_TableControlCheckBoxClick);
private void gridGroupingControl1_TableControlCheckBoxClick(object sender, GridTableControlCellClickEventArgs e)
{
    style = (GridTableCellStyleInfo)e.TableControl.GetTableViewStyleInfo(e.Inner.RowIndex, e.Inner.ColIndex);
    currentRecord = style.TableCellIdentity.DisplayElement.GetRecord();
    bool checkStatus = (style.Text == string.Empty || style.Text == "false");
    currentRecord.SetSelected(checkStatus);
}

 

Private Me.gridGroupingControl1.TableControlCheckBoxClick += New GridTableControlCellClickEventHandler(AddressOf gridGroupingControl1_TableControlCheckBoxClick)
Private Sub gridGroupingControl1_TableControlCheckBoxClick(ByVal sender As Object, ByVal e As GridTableControlCellClickEventArgs)
 style = CType(e.TableControl.GetTableViewStyleInfo(e.Inner.RowIndex, e.Inner.ColIndex), GridTableCellStyleInfo)
 currentRecord = style.TableCellIdentity.DisplayElement.GetRecord()
 Dim checkStatus As Boolean = (style.Text = String.Empty OrElse style.Text = "false")
 currentRecord.SetSelected(checkStatus)
End Sub

 

CurrentCellKeyDown event can be used for selecting the particular row by using space key on the checkbox.

this.gridGroupingControl1.TableControlCurrentCellKeyDown += gridGroupingControl1_TableControlCurrentCellKeyDown;
private void gridGroupingControl1_TableControlCurrentCellKeyDown(object sender, GridTableControlKeyEventArgs e)
{
    GridStyleInfo style = e.TableControl.CurrentCell.Renderer.CurrentStyle;
    // Make a selection while check the checkbox using key navigation
    if (e.Inner.KeyCode == Keys.Space && style.CellType == GridCellTypeName.CheckBox)
    {
        e.TableControl.RaiseCheckBoxClick(e.TableControl.CurrentCell.RowIndex, e.TableControl.CurrentCell.ColIndex, MouseEventArgs.Empty as MouseEventArgs);
    }
}  

 

Private Me.gridGroupingControl1.TableControlCurrentCellKeyDown += AddressOf gridGroupingControl1_TableControlCurrentCellKeyDown
Private Sub gridGroupingControl1_TableControlCurrentCellKeyDown(ByVal sender As Object, ByVal e As GridTableControlKeyEventArgs)
 Dim style As GridStyleInfo = e.TableControl.CurrentCell.Renderer.CurrentStyle
 ' Make a selection while check the checkbox using key navigation
 If e.Inner.KeyCode = Keys.Space AndAlso style.CellType Is GridCellTypeName.CheckBox Then
  e.TableControl.RaiseCheckBoxClick(e.TableControl.CurrentCell.RowIndex, e.TableControl.CurrentCell.ColIndex, TryCast(MouseEventArgs.Empty, MouseEventArgs))
 End If
End Sub

 

Screenshot

Shows the number of rows selected by clicking on checkbox cell

Samples:

C#: Row selection

VB: Row selection

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied