Articles in this section
Category / Section

How to put CheckBox in header cell for WinForms GridControl?

4 mins read

Adding checkbox in a header cell

GridControl:

In WinForms GridControl, to put a CheckBox in the GridControl, set the CellType to CheckBox and assign string values for the CheckedValue and UncheckedValue in the CheckBoxOptions property. The value of the CheckBox can be stored to a particular cell in the GridControl and the CheckBoxClick event gets triggered when the CheckBox is clicked. The following is the code example for GridControl.

C#

this.gridControl1[0,2].CellType = "CheckBox";
this.gridControl1[0,2].Description = "B";
this.gridControl1[0,2].CellAppearance = GridCellAppearance.Raised;

VB

Me.gridControl1(0,2).CellType = "CheckBox"
Me.gridControl1(0,2).Description = "B"
Me.gridControl1(0,2).CellAppearance = GridCellAppearance.Raised

GridDataBoundGrid:

To put a CheckBox in the GridDataBoundGrid, two events have to be used. The QueryCellInfo event is used to set the style properties and the SaveCellInfo event is used to save the cell's value. The value of the CheckBox cannot be stored in the GridDataBoundGrid, so any datatype/collection can be used to store the value. The CheckBoxClick event gets triggered when the CheckBox is clicked. In the following code example, the CheckBox is added to the Column header on the GridDataBoundGrid.

C#

private bool CheckBoxValue = false;   
private void Model_QueryCellInfo(object sender, GridQueryCellInfoEventArgs e)
{
   if(e.ColIndex > 0 && e.RowIndex == 0)
   {
      int colIndex1 = this.gridDataBoundGrid1.Binder.NameToColIndex("Column2");
      if(colIndex1 == e.ColIndex)
      {
         e.Style.Description = "Check";
         e.Style.CellValue = CheckBoxValue;
         e.Style.CellValueType = typeof(bool);
         e.Style.CheckBoxOptions = new GridCheckBoxCellInfo(true.ToString(), false.ToString(), "", true);
         e.Style.CellType = "CheckBox";
         e.Style.CellAppearance = GridCellAppearance.Raised;
         e.Style.Enabled = true;
      }
   }
}
private void Model_SaveCellInfo(object sender, GridSaveCellInfoEventArgs e)
{
   if(e.ColIndex > 0 && e.RowIndex == 0)
   {
      int colIndex1 = this.gridDataBoundGrid1.Binder.NameToColIndex("Column2");
      if(colIndex1 == e.ColIndex)
      {
         if(e.Style.CellValue != null)
  CheckBoxValue = (bool)e.Style.CellValue;
      }
   }
}

VB

Private CheckBoxValue As Boolean = False
Private Sub Model_QueryCellInfo(ByVal sender As Object, ByVal e As GridQueryCellInfoEventArgs)
  If e.ColIndex > 0 AndAlso e.RowIndex = 0 Then
    Dim colIndex1 As Integer = Me.gridDataBoundGrid1.Binder.NameToColIndex("Column2")
    If colIndex1 = e.ColIndex Then
      e.Style.Description = "Check"
      e.Style.CellValue = CheckBoxValue
      e.Style.CellValueType = GetType(Boolean)
      e.Style.CheckBoxOptions = New GridCheckBoxCellInfo(True.ToString(), False.ToString(), "", True)
      e.Style.CellType = "CheckBox"
      e.Style.CellAppearance = GridCellAppearance.Raised
      e.Style.Enabled = True
    End If
  End If
End Sub
Private Sub Model_SaveCellInfo(ByVal sender As Object, ByVal e As GridSaveCellInfoEventArgs)
  If e.ColIndex > 0 AndAlso e.RowIndex = 0 Then
    Dim colIndex1 As Integer = Me.gridDataBoundGrid1.Binder.NameToColIndex("Column2")
    If colIndex1 = e.ColIndex Then
      If e.Style.CellValue IsNot Nothing Then
        CheckBoxValue = CBool(e.Style.CellValue)
      End If
    End If
  End If
End Sub

In the following screenshot, the CheckBox is added to the Column header on the GridControl and GridDataBoundGrid.

Checkbox in the header for GridControl and GridDataBoundGrid

Figure 1: CheckBox in the Header for GridControl and GridDataBoundGrid

Samples:

C#: CheckBox HeaderCell CS

VB: CheckBox HeaderCell VB




Conclusion

I hope you enjoyed learning how to put CheckBox in the header cell for WinForms GridControl.

You can refer to our WinForms Grid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.
You can also explore our WinForms Grid example to understand how to present and manipulate data.

For current customers, you can check out our WinForms 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 WinForms Grid and other WinForms components.

If you have any queries or require clarifications, please let us know in comments 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