I want to add a checkbox in the header of a checkbox column in an SFDataGrid in C#

367 Views Asked by At

I am having trouble adding a checkbox that can check and uncheck all checkboxes in that column in a Syncfusion SFDataGrid in C#. This is my XAML code:

<syncfusion:SfDataGrid.Columns>                         
                            
                            <syncfusion:GridCheckBoxColumn Width="45" x:Name="SelectionColumn" HeaderText="" MappingName="bCreditoPreJudicial"  ShowToolTip="True" ShowHeaderToolTip="true" AllowEditing="true" AllowFiltering="false"  >                                
                                <syncfusion:GridCheckBoxColumn.HeaderTemplate>
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal" Width="18" Height="18">
                                            <CheckBox x:Name="chkHeader" Click="chkHeader_Click"/>
                                        </StackPanel>
                                    </DataTemplate>
                                </syncfusion:GridCheckBoxColumn.HeaderTemplate>                              

                            </syncfusion:GridCheckBoxColumn>

I cannot find any property of the CheckboxColumn that allows me to check and uncheck all checkboxes or any method to add it in the class. My SFDataGrid is named "DGCarteraActual" and the CheckboxColumn has a mapping name "bCreditoPreJudicial".

I tried various methods in the classes but it seems that my Syncfusion version does not allow me to use some properties. I expected the checkbox in the header of the column to check and uncheck all the checkboxes in the column.

2

There are 2 best solutions below

0
On

Add the GridCheckBoxSelectorColumn to the SfDataGrid columns. This allows you for the selection/deselection of rows in the datagrid.

<syncfusion:SfDataGrid>
    <syncfusion:SfDataGrid.Columns>
        <syncfusion:GridCheckBoxSelectorColumn MappingName="SelectorColumn" Width="30"/>
    <syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid>
0
On

Your requirement to display a checkbox on the header cell and check/uncheck all the checkboxes in that column can be achieved by using the HeaderTemplate and changing the underlying property based on the checkbox state of HeaderCheckBox. The below mentioned code snippet can help with this.

XAML Code Snippet:

<syncfusion:GridCheckBoxColumn Width="45" x:Name="SelectionColumn" HeaderText="" MappingName="BCreditoPreJudicial"  ShowToolTip="True" ShowHeaderToolTip="true" AllowEditing="true" AllowFiltering="false"  >
<syncfusion:GridCheckBoxColumn.HeaderTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal" Width="18" Height="18">
            <CheckBox x:Name="chkHeader" Click="chkHeader_Click"/>
        </StackPanel>
    </DataTemplate>
</syncfusion:GridCheckBoxColumn.HeaderTemplate>

C# Code Snippet:

//Event customization
private void chkHeader_Click(object sender, RoutedEventArgs e)
{
   //Here get the collection from the DataContext
   var collection = (this.DataContext as ViewModel).OrdersListDetails;

   foreach (var item in collection)
   {
      //check and uncheck all the checkboxes in the column by change the underlying property value based on HeaderCheckbox state
      (item as OrderInfo).BCreditoPreJudicial = (bool)(sender as CheckBox).IsChecked;
   }
}

Sample Link Demo