This is a migrated thread and some comments may be shown as answers.

Iterate/Loop through Datagrid rows to get the value of checkbox column using Silverlight/ c# wpf: NullReferenceException error

1 Answer 660 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Sarvesh
Top achievements
Rank 1
Sarvesh asked on 24 Apr 2013, 12:21 AM
I have a DataGrid in Silverlight xaml which contains three columns named Active(checkbox), name(Textcolumn), ID (Textcolumn) and a Save button outside the datagrid. I am getting the following error when I try to loop/iterate through the datagrid rows to get the value of the checkbox using GetCellContent. GetCellContent works fine for first 20 rows but gives the NullReferenceException error after that. Please help. 

    System.NullReferenceException: Object reference not set to an instance of an object.

XAML

    <StackPanel Orientation="Horizontal">
                        <Button Content ="Save" Click="SavePopUp_Click"/>
                        <TextBlock x:Name="GridHeader"  Style="{StaticResource GridHeaderStyle}" Margin="0,0,0,0" Width="445" Foreground="White" HorizontalAlignment="Right" TextAlignment="Center" Visibility="Collapsed"/>
    </StackPanel>
    
    <sdk:DataGrid  x:Name="dgloss">
    <sdk:DataGrid.RowDetailsTemplate>
                    <DataTemplate>
                          <Border>
                               <Border Margin="2" Padding="2" BorderBrush="#AAAAAA" BorderThickness="2" CornerRadius="4">
                      </Border>
                    </Border>
                    </DataTemplate>
            </sdk:DataGrid.RowDetailsTemplate>
    
            <sdk:DataGrid.Columns>
                           
             <!--<sdk:DataGridCheckBoxColumn  IsReadOnly="False" Header="Active" Binding="{Binding Active, Mode=TwoWay}" CanUserResize="False" />-->
                            <sdk:DataGridTemplateColumn Header ="Active">
                                <sdk:DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <CheckBox x:Name="chkcode" Tag="{Binding Active}" Checked="HandleCheck" Unchecked="HandleUnchecked" IsThreeState="False" HorizontalAlignment ="Center" VerticalAlignment="Center" IsChecked="{Binding Active, Mode=TwoWay}" />
                                    </DataTemplate>
                                </sdk:DataGridTemplateColumn.CellTemplate>
                                </sdk:DataGridTemplateColumn>
    
                                <sdk:DataGridTextColumn IsReadOnly="True" SortMemberPath="Name" Header="Name" Binding="{Binding Name, Mode=TwoWay}" Foreground="White"/>
                            <sdk:DataGridTextColumn x:Name="ID" IsReadOnly="True" SortMemberPath="ID" Header="ID" Binding="{Binding ID, Mode=TwoWay}" Foreground="White" Visibility="Visible"/>
             </sdk:DataGrid.Columns>
    
    </sdk:DataGrid>



---------------------------------------------------------------


**Save button Click event**


        private void SavePopUp_Click(object sender, RoutedEventArgs e)
                {
                   IEnumerable list = dgloss.ItemsSource as IEnumerable;
                   List<string> lstFile = new List<string>();
                   
                   foreach (var row in list)
                   {  
                       bool IsChecked = (bool)((CheckBox)dgloss.Columns[0].GetCellContent(row)).IsChecked;
                      
                       if (IsChecked)
                       {
                          string id = ((TextBlock)grdLossCodelist.Columns[2].GetCellContent(row)).Text;
                           lstFile.Add(id);
        
    }
    }    
    }

1 Answer, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 24 Apr 2013, 05:25 AM
Hello,

 This forum is for Telerik RadGridView. Maybe it will be better to post your questions related to Microsoft DataGrid to official Silverlight forums.

Greetings,
Vlad
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
GridView
Asked by
Sarvesh
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Share this question
or