CheckBox Checked and Un-Checked Events in Data Grid Header

In this article we will see how the Check Box check and uncheck events work in a Data Grid Header. In this article I have created the database using the code first approach and the main purpose of this article is to explain how to place a check box in a Data Grid header and when a header checkbox is checked then all the cells of that column will be in a checked state and when the header check box is un-checked all the cells of that column should be in the un-checked state. The following calm code will explain how to place a check box in a Data Grid header and cell of that specific column.

<Data Grid AutoGenerateColumns="False" Height="270" CanUserAddRows="False"     Name="dataGrid1" VerticalAlignment="Top" Width="481" >

   <DataGrid.Columns>

    <DataGridTextColumn Header="Name" Binding="{Binding Path=Name}"  Width="50"/>

     <DataGridTextColumn Header="Price" Binding="{Binding Path=Price}" Width="50" />

        <DataGridTextColumn Header="Unit" Binding="{Binding Path=Unit}" Width="50"/>

          <DataGridTemplateColumn>

             <DataGridTemplateColumn.Header>

             <CheckBox Content="Uncheck All Or Check All" Checked="CheckBox_Checked"    Unchecked="CheckBox_Unchecked"/>

                    </DataGridTemplateColumn.Header>

                    <DataGridTemplateColumn.CellTemplate>

                     <DataTemplate>

                     <CheckBox Name="chkDiscontinue" IsChecked="{Binding values,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"  />

                        </DataTemplate>

                    </DataGridTemplateColumn.CellTemplate>

                </DataGridTemplateColumn>

            </DataGrid.Columns>

        </DataGrid>
 
There are two events that take place in a Data Grid Header, namely checked event and unchecked event. I will explain both events one by one as shown below.

1. Check Box Checked Event in Data Grid Header Select Column

In this event, when you check the check box in the Data Grid header Select column then all the check boxes present in cells of that column will be in a checked state. See the following snapshot:

Checked Event in Data Grid Header

The code behind for the Check Box Checked Event in the Data Grid Header Select Column is:
 

private void CheckBox_Checked(object sender, RoutedEventArgs e)

{

    Database DB = new Database();

    Products products = new Products();

    foreach (var r in DB.prod)

    {

        r.values = true;

    }

    DB.SaveChanges();

    dataGrid1.ItemsSource = DB.prod.ToList();

}


2. Check Box Un-Checked Event in Data Grid Header Select Column

In this event when you un-check the check box in the Data Grid header Select column then all the check boxes present in the cells of that select column will be in the un-checked state. See the following snapshot:

UnChecked Event in Data Grid

The code behind for the Check Box Un-Checked Event in the Data Grid Header Column is:

private void CheckBox_Unchecked(object sender, RoutedEventArgs e)

{

    Database DB = new Database();

    Products products = new Products();

    foreach (var r in DB.prod)

    {

        r.values = false;

    }

    DB.SaveChanges();

    dataGrid1.ItemsSource = DB.prod.ToList();

}


Summary

In this article I have explained how check box events work for Data Grid headers. I have also uploaded the source code. If you have any doubts then please do comment. Thank you for reading my article