Home > Silverlight > DataGrid sample with SelectAll Checkbox

DataGrid sample with SelectAll Checkbox

September 19, 2008 lee Leave a comment Go to comments

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.

Categories: Silverlight Tags: , ,
  1. prasad
    November 17, 2009 at 4:38 pm | #1

    The code is not working with silverlight 3 in Visual Studio 2008; is there an updated code?

    • lee
      November 18, 2009 at 3:30 pm | #2

      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.

  1. September 23, 2008 at 11:37 am | #1
  2. September 25, 2008 at 6:04 am | #2