0

I have a DataGrid in wpf form. It has rows with checkboxes in it. I want to select/unselect all the rows with the header checkbox selected/unselected.

But I am getting this error: "Object reference not set to an instance of an object" on chk.IsChecked = false.

enter image description here

c# code:

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

xaml code is:

<Window x:Class="WpfApplication1.Grid"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Grid" Height="300" Width="300">
<Grid>
    <DataGrid x:Name="myDataGrid"
              VerticalAlignment="Top"
              Grid.Column="0"
              AutoGenerateColumns="False"
              LoadingRow="myDataGrid_LoadingRow"
              Loaded="myDataGrid_Loaded">
        <DataGrid.Columns>
            <DataGridTemplateColumn Width="80">
                <DataGridTemplateColumn.Header>
                    <CheckBox HorizontalAlignment="Center"
                              Click="chk_Click"
                              VerticalAlignment="Center"
                              Name="chckAll">
                    </CheckBox>
                </DataGridTemplateColumn.Header>

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

            <DataGridTextColumn Header="First Name"
                                Width="100"
                                Binding="{Binding FirstName}"></DataGridTextColumn>
            <DataGridTextColumn Header="Last Name"
                                Width="100"
                                Binding="{Binding LastName}"></DataGridTextColumn>
        </DataGrid.Columns> 
    </DataGrid>
</Grid>

Thanks in advance.

  • It looks more like your chk variable is null (you don't select your radiobutton). Can you put a breakpoint and check if it's null? – Sonhja Jan 18 '13 at 13:15
  • ya the chk variable is going null. I really don't know what's wrong with this code: CheckBox chk = (CheckBox)this.myDataGrid.Columns[0].GetCellContent(e.Row); – Newton Sheikh Jan 18 '13 at 13:16
  • To get your DataGridRow, you should follow this link: http://stackoverflow.com/questions/1976087/wpf-datagrid-set-selected-row – Sonhja Jan 18 '13 at 13:21
  • You mean that first load all the data into the grid, then iterate through each row to find out which of the row is selected/unselected, put them in the list and then fire the event for selecting/unselecting all the rows? – Newton Sheikh Jan 18 '13 at 13:29

1 Answers1

1

You can do what they say on this link.

For you should be something like this:

DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(dataGrid.SelectedIndex);
CheckBox chk = (CheckBox)this.myDataGrid.Columns[0].GetCellContent(e.Row);

I guess, not trying right now.

Community
  • 1
  • 1
Sonhja
  • 8,230
  • 20
  • 73
  • 131