)
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 place a CheckBox in a header cell of the WinForms GridGroupingControl?

Platform: WinForms |
Control: GridGroupingControl |
Published Date: April 30, 2011 |
Last Revised Date: December 23, 2019

Checkbox in header cell

A CheckBox can be placed in a header cell of the GridGroupingControl by handling the QueryCellStyleInfo event.

 

C#

void gridGroupingControl1_QueryCellStyleInfo(object sender, GridTableCellStyleInfoEventArgs e)
{
//Checks whether the cell is header cell or not.
if (e.TableCellIdentity.TableCellType == GridTableCellType.ColumnHeaderCell && e.TableCellIdentity.Column.Name == "Column2")
{
    e.Style.CellType = "CheckBox";
    e.Style.CellValueType = typeof(bool);
    e.Style.CellValue = CheckBoxValue;
    e.Style.ReadOnly = false;
    e.Style.CellAppearance = GridCellAppearance.Raised;
    e.Style.Enabled = true;
}
e.Handled = true;
}

VB

Private Sub gridGroupingControl1_QueryCellStyleInfo(ByVal sender As Object, ByVal e As GridTableCellStyleInfoEventArgs)
    'Checks whether the cell is header cell or not
    If e.TableCellIdentity.TableCellType = GridTableCellType.ColumnHeaderCell AndAlso e.TableCellIdentity.Column.Name = "Column2" Then
        e.Style.CellType = "CheckBox"
        e.Style.CellValueType = GetType(Boolean)
        e.Style.CellValue = CheckBoxValue
        e.Style.ReadOnly = False
        e.Style.CellAppearance = GridCellAppearance.Raised
        e.Style.Enabled = True
    End If
    e.Handled = True
End Sub

 

By default, the selected cell’s MouseController calls the Cellrenderer’s MouseUp that in turn raises the CheckBoxClick. The DragGroupHeader MouseController for the header cells does not call the Renderer’s MouseUp. So you have to explicitly call the Renderer’s Mouseup from the TableControl’s Mouseup checking for the MouseController type.

 

C#

//When the mouse button is clicked on the column, it checks and performs the desired operation.
void TableControl_MouseUp(object sender, MouseEventArgs e)
{
    int row, col;
    this.gridGroupingControl1.TableControl.PointToRowCol(new Point(e.X, e.Y), out row, out col);
    GridTableCellStyleInfo style = this.gridGroupingControl1.TableControl.Model[row, col];
    IMouseController controller;
    this.gridGroupingControl1.TableControl.MouseControllerDispatcher.HitTest(new Point(e.X, e.Y), e.Button, e.Clicks, out controller);
    //Checks whether the controller is DragGroupHeader or not
    if (controller != null && controller.Name == "DragGroupHeader" && style.TableCellIdentity.Column.Name == "Column2")
    {
        if (this.gridGroupingControl1.TableDescriptor.GroupedColumns.Count > 0 && col == this.gridGroupingControl1.TableDescriptor.GroupedColumns.Count + 1)
            this.gridGroupingControl1.TableControl.GetCellRenderer(row, col - this.gridGroupingControl1.TableDescriptor.GroupedColumns.Count).RaiseMouseUp(row, col - this.gridGroupingControl1.TableDescriptor.GroupedColumns.Count, e);
        else
            this.gridGroupingControl1.TableControl.GetCellRenderer(row, col).RaiseMouseUp(row, col, e);
 
    }
}

VB

'When the mouse button is clicked on the column , it checks and performs the desired operation.
Private Sub TableControl_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)
    Dim row, col As Integer
    Me.gridGroupingControl1.TableControl.PointToRowCol(New Point(e.X, e.Y), row, col)
    Dim style As GridTableCellStyleInfo = Me.gridGroupingControl1.TableControl.Model(row, col)
    Dim controller As IMouseController
    Me.gridGroupingControl1.TableControl.MouseControllerDispatcher.HitTest(New Point(e.X, e.Y), e.Button, e.Clicks, controller)
    'Checks whether the controller is DragGroupHeader or not
    If controller IsNot Nothing AndAlso controller.Name = "DragGroupHeader"   AndAlso && style.TableCellIdentity.Column.Name = "Column2"Then
        If Me.gridGroupingControl1.TableDescriptor.GroupedColumns.Count > 0 AndAlso col = Me.gridGroupingControl1.TableDescriptor.GroupedColumns.Count + 1 Then
            Me.gridGroupingControl1.TableControl.GetCellRenderer(row, col -   Me.gridGroupingControl1.TableDescriptor.GroupedColumns.Count).RaiseMouseUp(row, col - Me.gridGroupingControl1.TableDescriptor.GroupedColumns.Count, e)
        Else
            Me.gridGroupingControl1.TableControl.GetCellRenderer(row, col).RaiseMouseUp(row, col, e)
        End If
    End If
End Sub

 

By default, when the header is clicked, the sorting operation is done in the columns. To avoid this, the sorting is disabled in the header by providing the following code.

 

C#

this.gridGroupingControl1.TableDescriptor.Columns["Column2"].AllowSort = false;

 

VB

Me.gridGroupingControl1.TableDescriptor.Columns("Column2").AllowSort = False

 

The value of the CheckBox can be received through the SaveCellText handler that saves the value from the Grid. In the given sample, a temporary variable (CheckBoxValue) has been used to keep the modified value of the CheckBox and this value is refreshed through the QueryCellStyleInfo event.

 

C#

void gridGroupingControl1_SaveCellText(object sender, Syncfusion.Windows.Forms.Grid.GridCellTextEventArgs e)
{
    GridTableCellStyleInfo style = (GridTableCellStyleInfo)e.Style;
    //Checks whether the cell is header cell or not and the style is enabled
    if (style.Enabled && style.TableCellIdentity.TableCellType == GridTableCellType.ColumnHeaderCell)
    {
        this.CheckBoxValue = bool.Parse(e.Text);
        e.Handled = true;
    }
}

 

VB

Private Sub gridGroupingControl1_SaveCellText(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Grid.GridCellTextEventArgs)
    Dim style As GridTableCellStyleInfo = CType(e.Style, GridTableCellStyleInfo)
    'Checks whether the cell is header cell or not and the style is enabled
    If style.Enabled AndAlso style.TableCellIdentity.TableCellType = GridTableCellType.ColumnHeaderCell Then
        Me.CheckBoxValue = Boolean.Parse(e.Text)
        e.Handled = True
    End If
End Sub

 

When the CheckBox is clicked, it enables/disables all the check boxes in the column except the header cell.

 

C#

void gridGroupingControl1_TableControlCheckBoxClick(object sender, GridTableControlCellClickEventArgs e)
{
    GridTableCellStyleInfo style = (GridTableCellStyleInfo)e.TableControl.GetTableViewStyleInfo(e.Inner.RowIndex, e.Inner.ColIndex);
    if (style.Enabled)
    {
        int column = this.gridGroupingControl1.TableModel.NameToColIndex("Column2");
        Console.WriteLine("CheckBoxClicked");
        if (style.Enabled && style.TableCellIdentity.TableCellType == GridTableCellType.ColumnHeaderCell)
        {
            chk = (bool)this.gridGroupingControl1.TableModel[style.TableCellIdentity.RowIndex, column].CellValue;
            //Updates the changes
            e.TableControl.BeginUpdate();
            foreach (Record rec in e.TableControl.Table.Records)
            {
                int rowIndex = e.TableControl.Table.DisplayElements.IndexOf(rec);
                GridTableCellStyleInfo rowStyle = (GridTableCellStyleInfo)e.TableControl.GetTableViewStyleInfo(rowIndex, column);
                if (rowStyle.Enabled)
                    rec.SetValue("Column2", !chk);
            }
        e.TableControl.EndUpdate(true);
    }
    if (style.Enabled && (style.TableCellIdentity.TableCellType == GridTableCellType.RecordFieldCell || style.TableCellIdentity.TableCellType == GridTableCellType.AlternateRecordFieldCell) && style.TableCellIdentity.Column.Name == "Column2")
    {
        Record currentRecord = style.TableCellIdentity.DisplayElement.GetRecord();
        bool curStatus = bool.Parse(style.Text);
        //Updates the changes
        e.TableControl.BeginUpdate();
        //Gets the records
        foreach (Record r in e.TableControl.Table.Records)
        {
            if (r != currentRecord)
            {
                check = bool.Parse(r.GetValue(style.TableCellIdentity.Column.Name).ToString());
                ht.Add(check);
            }
        }
        e.TableControl.EndUpdate();
        if (ht.Contains(curStatus) && !ht.Contains(!curStatus))
        {
            if (curStatus)
                CheckBoxValue = false;
            this.gridGroupingControl1.TableModel[2, column].CellValue = curStatus;
        }
        else if (!ht.Contains(curStatus))
        {
            this.gridGroupingControl1.TableModel[2, column].CellValue = curStatus;
            CheckBoxValue = !curStatus;
        }
        ht.Clear();
    }
}
this.gridGroupingControl1.TableControl.Refresh();
}

 

VB

Private Sub gridGroupingControl1_TableControlCheckBoxClick(ByVal sender As Object, ByVal e As GridTableControlCellClickEventArgs)
Dim style As GridTableCellStyleInfo = CType(e.TableControl.GetTableViewStyleInfo(e.Inner.RowIndex, e.Inner.ColIndex), GridTableCellStyleInfo)
If style.Enabled Then
    Dim column As Integer = Me.gridGroupingControl1.TableModel.NameToColIndex("Column2")
    Console.WriteLine("CheckBoxClicked")
    If style.Enabled AndAlso style.TableCellIdentity.TableCellType = GridTableCellType.ColumnHeaderCell Then
        chk = CBool(Me.gridGroupingControl1.TableModel(style.TableCellIdentity.RowIndex, column).CellValue)
        'Updates the changes
        e.TableControl.BeginUpdate()
        For Each rec As Record In e.TableControl.Table.Records
            Dim rowIndex As Integer = e.TableControl.Table.DisplayElements.IndexOf(rec)
            Dim rowStyle As GridTableCellStyleInfo = CType(e.TableControl.GetTableViewStyleInfo(rowIndex, column), GridTableCellStyleInfo)
            If rowStyle.Enabled Then
                rec.SetValue("Column2", (Not chk))
            End If
            Next rec
            e.TableControl.EndUpdate(True)
 End If
 If style.Enabled AndAlso (style.TableCellIdentity.TableCellType = GridTableCellType.RecordFieldCell OrElse style.TableCellIdentity.TableCellType = GridTableCellType.AlternateRecordFieldCell) AndAlso style.TableCellIdentity.Column.Name = "Column2" Then
                Dim currentRecord As Record = style.TableCellIdentity.DisplayElement.GetRecord()
                Dim curStatus As Boolean = Boolean.Parse(style.Text)
                'Updates the changes
                e.TableControl.BeginUpdate()
                'Gets the records
                For Each r As Record In e.TableControl.Table.Records
                    If r IsNot currentRecord Then
                        check = Boolean.Parse(r.GetValue(style.TableCellIdentity.Column.Name).ToString())
                        ht.Add(check)
                    End If
                    Next r
                    e.TableControl.EndUpdate()
                    If ht.Contains(curStatus) AndAlso (Not ht.Contains((Not curStatus))) Then
                        If curStatus Then
                            CheckBoxValue = False
                        End If
                        Me.gridGroupingControl1.TableModel(2, column).CellValue = curStatus
                    ElseIf Not ht.Contains(curStatus) Then
                        Me.gridGroupingControl1.TableModel(2, column).CellValue = curStatus
                        CheckBoxValue = Not curStatus
                    End If
                    ht.Clear()
        End If
    End If
    Me.gridGroupingControl1.TableControl.Refresh()
End Sub

After applying the properties, the Grid is shown as follows,

Checkbox in the header cell

Figure 1: Enabling the CheckBoxes in the column

Samples:

C#: CheckBox_in_header

VB: CheckBox_in_header

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