Category / Section
How to select the record row while clicking on checkbox cell in WinForms GridGroupingControl?
1 min read
Select the record using checkbox
To select or deselect the whole row or record while checking or unchecking the check box in WinForms GridGroupingControl, the method SetSelected can be used through TableControlCheckBoxClick event. To have the same behavior by pressing Space key on the checkbox, TableControlCurrentCellKeyDown event is handled.
C#
this.gridGroupingControl1.TableControlCheckBoxClick += new GridTableControlCellClickEventHandler(gridGroupingControl1_TableControlCheckBoxClick);
this.gridGroupingControl1.TableControlCurrentCellKeyDown += gridGroupingControl1_TableControlCurrentCellKeyDown;
void gridGroupingControl1_TableControlCheckBoxClick(object sender, GridTableControlCellClickEventArgs e)
{
GridTableCellStyleInfo style = (GridTableCellStyleInfo)e.TableControl.GetTableViewStyleInfo(e.Inner.RowIndex, e.Inner.ColIndex);
Record currentRecord = style.TableCellIdentity.DisplayElement.GetRecord();
// Selection added for current record
if (this.gridGroupingControl1.TableModel[e.Inner.RowIndex, e.Inner.ColIndex].Text == "False")
currentRecord.SetSelected(style.Text == "False");
}
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);
}
}
VB
AddHandler gridGroupingControl1.TableControlCheckBoxClick, AddressOf gridGroupingControl1_TableControlCheckBoxClick
AddHandler Me.gridGroupingControl1.TableControlCurrentCellKeyDown, AddressOf gridGroupingControl1_TableControlCurrentCellKeyDown
Private Sub gridGroupingControl1_TableControlCheckBoxClick(ByVal sender As Object, ByVal e As GridTableControlCellClickEventArgs)
Dim style As GridTableCellStyleInfo = CType(e.TableControl.GetTableViewStyleInfo(e.Inner.RowIndex, e.Inner.ColIndex), GridTableCellStyleInfo)
Dim currentRecord As Record = style.TableCellIdentity.DisplayElement.GetRecord()
'Selection added for current record
If Me.gridGroupingControl1.TableModel(e.Inner.RowIndex, e.Inner.ColIndex).Text = "False" Then
currentRecord.SetSelected(style.Text = "False")
End If
End Sub
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
The
screenshot below illustrates selecting a row based on the checkbox selection.
Samples:
C#: Select Record
VB: Select Record