Articles in this section
Category / Section

How to set the checkbox in headercell to check or uncheck the whole column's checkbox status in WinForms GridControl or GridDataBoundGrid?

2 mins read

Checkbox column header

You can check or uncheck the Checkbox column in Grid by clicking the checkbox in column header. It can be achieved by using CheckBoxClick event.

The following code example explains how to add CheckBox in column header.

C#

//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;
    }
}

VB:

'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 Sub

 

Show the checkbox in header cell

Note:

The above same procedures are used in GridDataBoundGrid.

Sample Links:

C#: CheckBox_Columnheader_C#

VB: CheckBox_Columnheader_VB 

Conclusion

I hope you enjoyed learning about how to customize the color of metro theme in WinForms GridControl or GridDataBoundGrid.

You can refer to our WinForms Grid feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms Grid documentation 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 forumsDirect-Trac, or feedback portal. We are always happy to assist you!


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