Silverlight Datagrid with SelectAll CheckBox

     Yesterday one of my colleagues asked me about the way to get select all functionality in datagrid. I referred him to a very good article by Lee Corner.

     But before he worked on it I thought of implementing it on my own. The functionality is to have all checkbox checked if header checkbox is checked and if any one row of datagrid checkbox is unchecked then header checkbox must be unchecked.

     When I checked with Lee’s article I found that if you unchecked any row of datagrid then header checkbox must be unchecked and that was not there in that article. So, I did some search on google and found the solution for it.
Here is the xaml code:



<data:DataGrid x:Name=″myDataGrid″ AutoGenerateColumns=″False″>
        <data:DataGrid.Columns>
             <data:DataGridTemplateColumn>
                   <data:DataGridTemplateColumn.HeaderStyle>
                        <Style TargetType=″dataprimitives:DataGridColumnHeader″>
                             <Setter Property="Template">
                                  <Setter.Value>
                                       <ControlTemplate>
                                            <Grid>
                                                 <Grid.ColumnDefinitions>
                                                      <ColumnDefinition> </ColumnDefinition>
                                                 </Grid.ColumnDefinitions>
                                                <CheckBox x:Name="chkAll" Click="chkAll_Click" Grid.Column="0"/>
                                           </Grid>
                                      </ControlTemplate>
                                 </Setter.Value>
                            </Setter>
                       </Style>
                  </data:DataGridTemplateColumn.HeaderStyle>
                  <data:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                              <CheckBox x:Name="chkBoxID" Click="chkBoxID_Click" Tag="{Binding Path=ID}" />
                        </DataTemplate>
                 </data:DataGridTemplateColumn.CellTemplate>
          </data:DataGridTemplateColumn>
      <data:DataGridTextColumn Header="First Name" Width="150" Binding="{Binding Path=FirstName}" FontSize="10" />
       <data:DataGridTextColumn Header="Last Name" Width="150" Binding="{Binding Path=LastName}" FontSize="10" />
   </data:DataGrid.Columns>
</data:DataGrid>


In Code Behind:
I’m using employee class to get ID, First Name and Last Name.



public class Employee
{
         public int ID { get; set; }
         public string FirstName { get; set; }
         public string LastName { get; set; }
         public Employee(int id, String firstName, String lastName)
         {
                this.ID = id;
                this.FirstName = firstName;
                this.LastName = lastName;
         }
         public static List GetSampleEmployeeList()
         {
                return new List(new Employee[4] {
                                     new Employee(1, "Lalit", "Sharma"),
                                     new Employee(2,"Abhijit", "Rajhans"),
                                     new Employee(3,"Nikhil", "Pai"),
                                     new Employee(4,"Asif", "Shaikh")
                   });
         }
}


List employee = null;
List checkboxes = new List();


public DatagridCheckbox()
{
         InitializeComponent();
         employee = Employee.GetSampleEmployeeList();
         myDataGrid.ItemsSource = employee;
         myDataGrid.LoadingRow += new EventHandler(myDataGrid_LoadingRow);
}
void myDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
         CheckBox chk = myDataGrid.Columns[0].GetCellContent(e.Row) as CheckBox;
         chk.IsChecked = false;
         checkboxes.Add(chk);
}


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


private void chkBoxID_Click(object sender, RoutedEventArgs e)
{
         CheckBox cb = GetCheckBoxWithParent(this.myDataGrid, typeof(CheckBox), "chkAll");
         if (cb != null)
         {

               cb.IsChecked = false;
         }
}


private CheckBox GetCheckBoxWithParent(UIElement parent, Type targetType, string CheckBoxName)
{
         if (parent.GetType() == targetType && ((CheckBox)parent).Name == CheckBoxName)
         {
                  return (CheckBox)parent;
         }
         CheckBox result = null;
         int count = VisualTreeHelper.GetChildrenCount(parent);
         for (int i = 0; i < count; i++)
         {
              UIElement child = (UIElement)VisualTreeHelper.GetChild(parent, i);
              if (GetCheckBoxWithParent(child, targetType, CheckBoxName) != null)
              {
                    result = GetCheckBoxWithParent(child, targetType, CheckBoxName);
                    break;
              }
       }
       return result;
}


Happy Coding :)




Note: I apologize for any mistakes in advance as this being my first post.


Comments are always welcome.