1

I am facing little issues on working with data grid in WPF. Previously I was comfortable with Windows forms.

I have added a checkbox column in data grid and added a checkbox in the header.

Now How do I write code to select/unselect all checkbox on selecting checkbox header?

I tried all the possible answers from other posts but I am unable to success on it. I am little confused somewhere.

<DataGrid AutoGenerateColumns="True" Height="204" HorizontalAlignment="Left" Margin="38,162,0,48" Name="dataGrid1" VerticalAlignment="Stretch" Width="729" AreRowDetailsFrozen="False"  EnableColumnVirtualization="False" IsManipulationEnabled="False" CanUserAddRows="False" AutoGeneratingColumn="dataGrid1_AutoGeneratingColumn" RowHeight="26" ColumnHeaderHeight="26" FontSize="15">
    <DataGrid.Columns>
        <DataGridCheckBoxColumn >
            <DataGridCheckBoxColumn.Header>
                <CheckBox Name="SelectAll" ></CheckBox>
            </DataGridCheckBoxColumn.Header>
        </DataGridCheckBoxColumn>
    </DataGrid.Columns>
</DataGrid>

What should I do next?

1

2 Answers 2

2

In WPF, you're supposed to use data binding. So in your case, you should data bind a bool property to the CheckBox in your Header and another for each item in the collection:

<DataGridCheckBoxColumn Binding="{Binding IsSelected, Mode=TwoWay}">
    <DataGridCheckBoxColumn.Header>
        <CheckBox Name="SelectAll" IsChecked="{Binding AreAllCheckBoxesChecked, 
            RelativeSource={RelativeSource AncestorType={x:Type YourPrefix:YourWindow}}}" />
</DataGridCheckBoxColumn>

In this example, the items in your collection will need to have an IsSelected property to data bind to the DataGridCheckBoxColumn and your view model or code behind will need an AreAllCheckBoxesChecked property to data bind to the Header Checkbox.IsChecked property. Then, it's a simple matter of updating the IsSelected property of each collection item in the setter of the AreAllCheckBoxesChecked property:

public bool AreAllCheckBoxesChecked
{
    get { return areAllCheckBoxesChecked; }
    set 
    {
        areAllCheckBoxesChecked = value;
        foreach (YourDataType item in YourCollection)
        {
            item.IsSelected = value;
        }
        NotifyPropertyChanged("AreAllCheckBoxesChecked"); 
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

What should I give for 'YourDataType' and 'YourCollection'? public bool AreAllCheckBoxesChecked { get { return AreAllCheckBoxesChecked; } set { AreAllCheckBoxesChecked = value; foreach (DataGridRow item in dataGrid1.Items) { item.IsSelected = value; } } } This is not working. What should I correct?
I thought that would be self explanatory... YourDataType is the type of class that each of your items is and YourCollection is the name of the collection property that you are data binding to the DataGrid.ItemsSource property. As for what you should correct... just look at my code again and compare it to yours... there should be a private bool areAllCheckBoxesChecked variable as well.
I am not binding any ItemsSource for data grid. I am binding it in code. dataGrid1.ItemsSource=dataTable.AsDataView();
I am binding it in code... no you're not... that's not a Binding. You need to properly data bind the ItemsSource property to a collection property for this to work (which you should be doing anyway). You can still setup a Binding in code.
0

You need to bind checkboxes to some properties of VM if you are following MVVM, if not, instead of IsSelected, you can have an event handler for checked and unchecked events.

<DataGridTemplateColumn>
                            <DataGridTemplateColumn.HeaderTemplate>
                                <DataTemplate>
                                    <CheckBox IsChecked="{Binding SelectAll}"/>
                                </DataTemplate>
                            </DataGridTemplateColumn.HeaderTemplate>
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <CheckBox IsChecked="{Binding IsSelected}"/>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>

2 Comments

Can you please show me some code for event handlers for checked and unchecked events? This is the place I am struck.
Trust me don't go that route, try and implement using MVVM read about it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.