none
select all Checkbox wpf datagrid C# RRS feed

  • Question

  •  Hi ,

         Im facing an issue with  Wpf DataGrid Checkboxes C#.

    Im not finding a way to select all cell template checkboxes when the header template checkbox is selected.

    This is my code:

    XAML:

    <DataGrid AutoGenerateColumns="True" SelectionMode="Single" Height="307" HorizontalAlignment="Left" Margin="40,166,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="296" Background="#FF2689E2"   >

                            <DataGrid.Columns>

                                <DataGridTemplateColumn Selector.IsSelected="True">

                                      <DataGridTemplateColumn.CellTemplate>

                                        <DataTemplate>

                                            <CheckBox  Name="Chk" DataContext="{Binding}" Checked="Chk_checked" IsChecked="False" Unchecked="Chk_unchecked" />

                                        </DataTemplate>

                                    </DataGridTemplateColumn.CellTemplate>

                                    <DataGridTemplateColumn.HeaderTemplate>

                                        <DataTemplate>

                                            <CheckBox Name="ChkAll" DataContext="{Binding}"  Checked="ChkAll_Checked"  Unchecked="ChkAll_Unchecked"  IsThreeState="False" Padding="4,3,4,3" HorizontalContentAlignment="Left" HorizontalAlignment="Center"  />

                                             

                                        </DataTemplate>

                                    </DataGridTemplateColumn.HeaderTemplate>

                                </DataGridTemplateColumn>

                            </DataGrid.Columns>

                        </DataGrid>

    code behind:

    private void Chk_checked(object sender, RoutedEventArgs e)

    {

    CheckBox chk = (CheckBox)sender;

    DataRowView l = (DataRowView)chk.DataContext;

    object[] arr = l.Row.ItemArray;

    list1.Add(arr);

    }

    private void Chk_unchecked(object sender, RoutedEventArgs e)

    {

    CheckBox chk = (CheckBox)sender;

    DataRowView l = (DataRowView)chk.DataContext;

    object[] arr = l.Row.ItemArray;

    for (int i = 0; i < list1.Count; i++)

    {

    // bool result = CompareObjects(list[i],arr);

    bool result = CompareObjects(list1[i], arr);

    if (result)

    list1.RemoveAt(i);

    }

    }

       

    private void ChkAll_Checked(object sender, RoutedEventArgs e)

    {

    list1.Clear();

    for (int i = 0; i < dataGrid1.Items.Count - 1; i++)

    {

    CheckBox chk = new CheckBox();

    object[] obj = ((DataRowView)dataGrid1.Items[i]).Row.ItemArray;

    chk.IsChecked = true;

    //chk.IsChecked.Value = true;

    list1.Add(obj);

    }

    }

    This is my code:

    Im getting the datagrid values in a list .

    What changes should i do to make this code work..?

    or can any please suggest some code..?

    Thanx

    Thursday, May 10, 2012 12:56 PM

Answers