|
1 | | -# how-to-load-checkbox-in-header-cells |
2 | | -This example illustrates how to load checkbox in header cells of GridDataBoundGrid control |
| 1 | +# How to Add a CheckBox to the Header Cell in the WinForms GridDataBoundGrid? |
| 2 | + |
| 3 | +This example illustrates how to add a checkbox to the header cells in the [WinForms GridDataBoundGrid](https://help.syncfusion.com/windowsforms/classic/databoundgrid/overview). |
| 4 | + |
| 5 | +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**. |
| 6 | + |
| 7 | +#### C# |
| 8 | + |
| 9 | +``` csharp |
| 10 | +private bool CheckBoxValue = false; |
| 11 | +private void Model_QueryCellInfo(object sender, GridQueryCellInfoEventArgs e) |
| 12 | +{ |
| 13 | + if(e.ColIndex > 0 && e.RowIndex == 0) |
| 14 | + { |
| 15 | + int colIndex1 = this.gridDataBoundGrid1.Binder.NameToColIndex("Column2"); |
| 16 | + if(colIndex1 == e.ColIndex) |
| 17 | + { |
| 18 | + e.Style.Description = "Check"; |
| 19 | + e.Style.CellValue = CheckBoxValue; |
| 20 | + e.Style.CellValueType = typeof(bool); |
| 21 | + e.Style.CheckBoxOptions = new GridCheckBoxCellInfo(true.ToString(), false.ToString(), "", true); |
| 22 | + e.Style.CellType = "CheckBox"; |
| 23 | + e.Style.CellAppearance = GridCellAppearance.Raised; |
| 24 | + e.Style.Enabled = true; |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +private void Model_SaveCellInfo(object sender, GridSaveCellInfoEventArgs e) |
| 30 | +{ |
| 31 | + if(e.ColIndex > 0 && e.RowIndex == 0) |
| 32 | + { |
| 33 | + int colIndex1 = this.gridDataBoundGrid1.Binder.NameToColIndex("Column2"); |
| 34 | + if(colIndex1 == e.ColIndex) |
| 35 | + { |
| 36 | + if(e.Style.CellValue != null) |
| 37 | + CheckBoxValue = (bool)e.Style.CellValue; |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +#### VB |
| 44 | + |
| 45 | +``` vb |
| 46 | +Private CheckBoxValue As Boolean = False |
| 47 | +Private Sub Model_QueryCellInfo(ByVal sender As Object, ByVal e As GridQueryCellInfoEventArgs) |
| 48 | + If e.ColIndex > 0 AndAlso e.RowIndex = 0 Then |
| 49 | + Dim colIndex1 As Integer = Me.gridDataBoundGrid1.Binder.NameToColIndex("Column2") |
| 50 | + If colIndex1 = e.ColIndex Then |
| 51 | + e.Style.Description = "Check" |
| 52 | + e.Style.CellValue = CheckBoxValue |
| 53 | + e.Style.CellValueType = GetType(Boolean) |
| 54 | + e.Style.CheckBoxOptions = New GridCheckBoxCellInfo(True.ToString(), False.ToString(), "", True) |
| 55 | + e.Style.CellType = "CheckBox" |
| 56 | + e.Style.CellAppearance = GridCellAppearance.Raised |
| 57 | + e.Style.Enabled = True |
| 58 | + End If |
| 59 | + End If |
| 60 | +End Sub |
| 61 | + |
| 62 | +Private Sub Model_SaveCellInfo(ByVal sender As Object, ByVal e As GridSaveCellInfoEventArgs) |
| 63 | + If e.ColIndex > 0 AndAlso e.RowIndex = 0 Then |
| 64 | + Dim colIndex1 As Integer = Me.gridDataBoundGrid1.Binder.NameToColIndex("Column2") |
| 65 | + If colIndex1 = e.ColIndex Then |
| 66 | + If e.Style.CellValue IsNot Nothing Then |
| 67 | + CheckBoxValue = CBool(e.Style.CellValue) |
| 68 | + End If |
| 69 | + End If |
| 70 | + End If |
| 71 | +End Sub |
| 72 | +``` |
| 73 | + |
| 74 | +In the following screenshot, the **CheckBox** is added to the Column header on the **GridControl** and **GridDataBoundGrid**. |
| 75 | + |
| 76 | + |
0 commit comments