How to set the checkbox in headercell to checking status WinForms GridControl?
Checkbox column header
You can check
or uncheck the Checkbox column in the WinForms GridControl by clicking the checkbox in the
column header. It can be achieved by using the CheckBoxClick event.
The following code example explains how to add a CheckBox in the column header.
//Hooks CheckBox click event.
this.gridControl1.CheckBoxClick += gridControl1_CheckBoxClick;
//Changes Header CellType.
this.gridControl1[0, 1].CellType = GridCellTypeName.CheckBox;
//header cell as checkbox
this.gridControl1[0, 1].CellValue = "false";
this.gridControl1[0, 1].CheckBoxOptions = new GridCheckBoxCellInfo("true", "false", "false", true);
//Assigns checked and unchecked values
//Sets the Column Cell Type as CheckBox
this.gridControl1.ColStyles[1].CellType = GridCellTypeName.CheckBox;
//Column celltype as checkbox
this.gridControl1.ColStyles[1].HorizontalAlignment = GridHorizontalAlignment.Center;
this.gridControl1.ColStyles[1].CellValue = "false";
this.gridControl1.ColStyles[1].CheckBoxOptions = new GridCheckBoxCellInfo("true", "false", "false", true);
//Assigns checked and unchecked values
void gridControl1_CheckBoxClick(object sender, GridCellClickEventArgs e)
{
if (e.ColIndex == 1 && e.RowIndex == 0)
{
if (this.gridControl1[e.RowIndex, e.ColIndex].CellValue.ToString() == "false")
{
for (int i = 1; i <= this.gridControl1.ColCount; i++)
this.gridControl1[i, e.ColIndex].CellValue = "true";
}
else
{
for (int i = 1; i <= this.gridControl1.ColCount; i++)
this.gridControl1[i, e.ColIndex].CellValue = "false";
}
}
if (e.RowIndex == 2 && e.ColIndex == 1)
//customizes your condition
{
MessageBox.Show("Cannot perform checkbox operation");
e.Cancel = true;
}
}'Hooks CheckBox click event.
Private Me.gridControl1.CheckBoxClick += AddressOf gridControl1_CheckBoxClick
'Changes Header CellType.
Private Me.gridControl1(0, 1).CellType = GridCellTypeName.CheckBox
'header cell as checkbox
Private Me.gridControl1(0, 1).CellValue = "false"
Private Me.gridControl1(0, 1).CheckBoxOptions = New GridCheckBoxCellInfo("true", "false", "false", True)
'Assigns checked and unchecked values
'Sets the Column Cell Type as CheckBox
Private Me.gridControl1.ColStyles(1).CellType = GridCellTypeName.CheckBox
'Column celltype as checkbox
Private Me.gridControl1.ColStyles(1).HorizontalAlignment = GridHorizontalAlignment.Center
Private Me.gridControl1.ColStyles(1).CellValue = "false"
Private Me.gridControl1.ColStyles(1).CheckBoxOptions = New GridCheckBoxCellInfo("true", "false", "false", True)
'Assigns checked and unchecked values
Private Sub gridControl1_CheckBoxClick(ByVal sender As Object, ByVal e As GridCellClickEventArgs)
If e.ColIndex = 1 AndAlso e.RowIndex = 0 Then
If Me.gridControl1(e.RowIndex, e.ColIndex).CellValue.ToString() = "false" Then
For i As Integer = 1 To Me.gridControl1.ColCount
Me.gridControl1(i, e.ColIndex).CellValue = "true"
Next i
Else
For i As Integer = 1 To Me.gridControl1.ColCount
Me.gridControl1(i, e.ColIndex).CellValue = "false"
Next i
End If
End If
If e.RowIndex = 2 AndAlso e.ColIndex = 1 Then
'customizes your condition
MessageBox.Show("Cannot perform checkbox operation")
e.Cancel = True
End If
End SubThe
screenshot below illustrates the checkbox in the column header
Note:
The same procedures are used in GridDataBoundGrid.
Sample Links:
Conclusion
I hope you enjoyed learning about how to set the checkbox in header cell to checking status WinForms GridControl
You can refer to 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.
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!