)
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 set the checkbox in headercell to check or uncheck the whole column's checkbox status in WinForms GridControl or GridDataBoundGrid?

Platform: WinForms |
Control: GridDataBoundGrid(Classic) |
Published Date: April 21, 2015 |
Last Revised Date: February 11, 2020

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

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