DataGrid sample with SelectAll Checkbox

Here is a one way of getting the SelectAll functionality in DataGrid

the DataGrid is defined like this

 <my:DataGrid x:Name=”myDataGrid”
                     VerticalAlignment=”Top”
                     Width=”350″
                     Height=”300″
                     Grid.Column=”0″
                     AutoGenerateColumns=”False”>
            <my:DataGrid.Columns>
                <my:DataGridTemplateColumn Width=”80″>
                    <my:DataGridTemplateColumn.Header>
                        <CheckBox HorizontalAlignment=”Center”
                                  Click=”chk_Click”
                                  VerticalAlignment=”Center”
                                  x:Name=”chkAll”></CheckBox>

                    </my:DataGridTemplateColumn.Header>
                    <my:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox x:Name=”chk”
                                      HorizontalAlignment=”Center”
                                      HorizontalContentAlignment=”Center”></CheckBox>
                        </DataTemplate>
                    </my:DataGridTemplateColumn.CellTemplate>

                </my:DataGridTemplateColumn>
                <my:DataGridTextColumn Header=”First Name”
                                       Width=”100″
                                       DisplayMemberBinding=”{Binding FirstName}”
                                       FontSize=”20″ />
                <my:DataGridTextColumn Header=”Last Name”
                                       Width=”100″
                                       DisplayMemberBinding=”{Binding LastName}”
                                       FontSize=”20″ />
            </my:DataGrid.Columns>
        </my:DataGrid>

when the rows are being loaded we keep a track of all the checkboxes in the LoadingRow eventhandler

 void myDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            CheckBox chk = myDataGrid.Columns[0].GetCellContent(e.Row) as CheckBox;
            chk.IsChecked = false;
            checkboxes.Add(chk);
        }

and when the checkbox in the header is clicked we check/uncheck each of the items in the list of checkboxes.

 void chk_Click(object sender, RoutedEventArgs e)
        {
            CheckBox chk = sender as CheckBox;
            bool check   = chk.IsChecked.Value;
            foreach (CheckBox chkbox in checkboxes)
                chkbox.IsChecked = check;          
        }

you can download the code here

Note:
This will work only if datagrid has no scrolling.
I will post couple more, one that will work with scrolling and other changing the class to add a property so we can use databinding.

Advertisements

6 thoughts on “DataGrid sample with SelectAll Checkbox

    1. Hi,
      There will be issues with having select all checkbox, because of the Virtualization, it is best to have a property that can be bound to the check box and do this using databinding by setting the property . I dont think we can genralize this kind of behaviour.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s