Datagrid Column header should check / uncheck CheckBox’s state depending upon whether all CheckBoxes of a DataGridView column are checked or unchecked

19,548
//this event is for **Checked and UnChecked** of up check box (cbxall)
private void UpCheckbox_Checked(object sender, RoutedEventArgs e)
{
    //checkBox1 = cbxall (your up checkbox)
    if (checkBox1.IsChecked == true)
    {
        dataGrid1.Items.OfType<YourClass>().ToList().ForEach(x => x.IsChecked = true);
    }
    else
    {
        dataGrid1.Items.OfType<YourClass>().ToList().ForEach(x => x.IsChecked = false);
    }
}

//this event is for all other check box
//**Checked and UnChecked** of all other check box is this event
private void OtherCheckbox_Checked(object sender, RoutedEventArgs e)
{
    //checkBox1 = cbxall (your up checkbox)
    if (dataGrid1.Items.OfType<YourClass>().All(x => x.IsChecked == true))
    {
        checkBox1.IsChecked = true;
    }
    else if (dataGrid1.Items.OfType<YourClass>().All(x => x.IsChecked == false))
    {
        checkBox1.IsChecked = false;
    }
    else
    {
        checkBox1.IsChecked = null;
    }
}
Share:
19,548
iYadav
Author by

iYadav

SSE at an MNC, Love coding and Interested in C#/WPF/MVVM.

Updated on July 20, 2022

Comments

  • iYadav
    iYadav almost 2 years

    enter image description here The problem i'm stuck with is related to checkbox in DataGrid(WPF). I've attached the screenshot for better understanding of the problem.

    Problem: The DataHeader Column Checkbox is checked even when one of the child is Unchecked. I expect the solution to fix this so that when one of the child is unchecked explicitly by the user, The ALL(Column Header) should be unchecked implicitly.

    Please help guys... Thank You Plz check the link. i want the solution to work like this. http://www.codeproject.com/Articles/42437/Toggling-the-States-of-all-CheckBoxes-Inside-a-Dat#

    <dg:DataGrid.Columns>
        <dg:DataGridCheckBoxColumn Binding="{Binding Check}" IsThreeState="True" Width="50">
            <dg:DataGridCheckBoxColumn.HeaderTemplate>
                <DataTemplate x:Name="dtAllChkBx">
                    <CheckBox Name="cbxAll" Content="{x:Static properties:Resources.lblAll}"
                              Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" />
                </DataTemplate>
            </dg:DataGridCheckBoxColumn.HeaderTemplate>
        </dg:DataGridCheckBoxColumn>
    

    .

    private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
    {
        unchck_all_prd();
        dgEnggAcc.Items.Refresh();
    }
    
    private void unchck_all_prd()
    {
        for (int i = 0; i < engg_list.Count; i++)
        {
            engg_list[i].Check = false;
        }
    }
    
    private void chck_all_prd()
    {
        for (int i = 0; i < engg_list.Count; i++)
        {
            engg_list[i].Check = true;
        }
    }
    
    public class EnggLst : ObservableCollection<EnggLst>
    {
        public bool Check { get; set; }
    }
    
  • Yinda Yin
    Yinda Yin about 6 years
    Checking for equality with true is redundant. Just say if (ckbox.IsChecked)
  • Admin
    Admin about 6 years
    IsChecked is a nullable bool, so you have to explicitly check its value with ckbox.IsChecked == true or coalesce it with ckbox.IsChecked ?? false
  • Darlan Dieterich
    Darlan Dieterich almost 6 years
    NO, in type CheckBox the property IsChecked is possible for null, then, it's necessary specify true, false or null. try for reality.