Home > Back-end >  Iterate over datagrid rows to get the value of radio column getting Null Exception after 20 rows
Iterate over datagrid rows to get the value of radio column getting Null Exception after 20 rows

Time:09-21

I have an issue with my datagrid when trying to get a value of a cell. Everything working fine until I'm reaching rows #20. I'm getting a null exception. Is it a limit of datagrid rows in iteration? Because in the visual, I can see all the rows.

I didn't have issue in the past with Datagrid but they were not any radiobutton or checkbox column, perhaps an issue with their rendering?

Here's my code:

XAML

<DataGrid x:Name="datagrid_wafer_300" Margin="5" HeadersVisibility="Column" GridLinesVisibility="None">
                        <DataGrid.Columns>
                            <DataGridTextColumn Binding="{Binding ID}" Header="ID" Width="140" IsReadOnly="True" Visibility="Hidden">
                                <DataGridTextColumn.HeaderStyle>
                                    <Style TargetType="{x:Type DataGridColumnHeader}">
                                        <Setter Property="HorizontalAlignment" Value="Stretch"/>
                                        <Setter Property="HorizontalContentAlignment" Value="Center"/>
                                        <Setter Property="FontWeight" Value="Bold" />
                                    </Style>
                                </DataGridTextColumn.HeaderStyle>
                                <DataGridTextColumn.ElementStyle>
                                    <Style>
                                        <Setter Property="TextBlock.TextWrapping" Value="Wrap"/>
                                        <Setter Property="TextBlock.TextAlignment" Value="Center"/>
                                        <Setter Property="TextBlock.Margin" Value="10,0"/>
                                        <Setter Property="TextBlock.VerticalAlignment" Value="Center"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTemplateColumn x:Name="column_check_300" Width="10" IsReadOnly="True">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <RadioButton HorizontalAlignment="Center" VerticalAlignment="Center" GroupName="{Binding Part_Number}"  IsChecked="{Binding IsIncluded}"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                            <DataGridTextColumn Binding="{Binding Part_Number}" Header="Part Number" Width="140" IsReadOnly="True">
                                <DataGridTextColumn.HeaderStyle>
                                    <Style TargetType="{x:Type DataGridColumnHeader}">
                                        <Setter Property="HorizontalAlignment" Value="Stretch"/>
                                        <Setter Property="HorizontalContentAlignment" Value="Center"/>
                                        <Setter Property="FontWeight" Value="Bold" />
                                    </Style>
                                </DataGridTextColumn.HeaderStyle>
                                <DataGridTextColumn.ElementStyle>
                                    <Style>
                                        <Setter Property="TextBlock.TextWrapping" Value="Wrap"/>
                                        <Setter Property="TextBlock.TextAlignment" Value="Center"/>
                                        <Setter Property="TextBlock.Margin" Value="10,0"/>
                                        <Setter Property="TextBlock.VerticalAlignment" Value="Center"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding Description}" Header="Description" Width="*" IsReadOnly="True">
                                <DataGridTextColumn.HeaderStyle>
                                    <Style TargetType="{x:Type DataGridColumnHeader}">
                                        <Setter Property="HorizontalAlignment" Value="Stretch"/>
                                        <Setter Property="HorizontalContentAlignment" Value="Center"/>
                                        <Setter Property="FontWeight" Value="Bold" />
                                    </Style>
                                </DataGridTextColumn.HeaderStyle>
                                <DataGridTextColumn.ElementStyle>
                                    <Style>
                                        <Setter Property="TextBlock.TextWrapping" Value="Wrap"/>
                                        <Setter Property="TextBlock.TextAlignment" Value="Center"/>
                                        <Setter Property="TextBlock.Margin" Value="10,0"/>
                                        <Setter Property="TextBlock.VerticalAlignment" Value="Center"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTemplateColumn x:Name="column_exclude_300" Header="Exclude?" Width="60" IsReadOnly="True">
                                <DataGridTemplateColumn.HeaderStyle>
                                    <Style TargetType="{x:Type DataGridColumnHeader}">
                                        <Setter Property="HorizontalAlignment" Value="Stretch"/>
                                        <Setter Property="HorizontalContentAlignment" Value="Center"/>
                                        <Setter Property="VerticalAlignment" Value="Center"/>
                                        <Setter Property="FontWeight" Value="Bold" />
                                    </Style>
                                </DataGridTemplateColumn.HeaderStyle>
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <RadioButton HorizontalAlignment="Center" VerticalAlignment="Center" GroupName="{Binding Part_Number}" IsChecked="{Binding IsIncluded}"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                        </DataGrid.Columns>
                    </DataGrid>

C# code:

private void Clear_Datagrid()
{
        for (int i = 0; i < datagrid_wafer_200.Items.Count; i  )
        {
            var item = datagrid_wafer_200.Items[i];
            FrameworkElement elmtTest = datagrid_wafer_200.Columns[1].GetCellContent(item);
            var radio = FindVisualChild<RadioButton>(elmtTest);
            var wafer = datagrid_wafer_200.Columns[2].GetCellContent(item) as TextBlock;
            FrameworkElement elmtTest1 = datagrid_wafer_200.Columns[4].GetCellContent(item);
            var exclude = FindVisualChild<RadioButton>(elmtTest1);

            radio.IsChecked = false;
            exclude.IsChecked = false;
        }

        for (int i = 0; i < datagrid_wafer_300.Items.Count; i  )
        {
            var item = datagrid_wafer_300.Items[i];
            FrameworkElement elmtTest = datagrid_wafer_300.Columns[1].GetCellContent(item);

            var radio = FindVisualChild<RadioButton>(elmtTest);
            var wafer = datagrid_wafer_300.Columns[2].GetCellContent(item) as TextBlock;
            FrameworkElement elmtTest1 = datagrid_wafer_300.Columns[4].GetCellContent(item);
            var exclude = FindVisualChild<RadioButton>(elmtTest1);

            radio.IsChecked = false;
            exclude.IsChecked = false;
        }
}

Thank you

CodePudding user response:

Is it a limit of datagrid rows in iteration?

It's called UI virtualization and means that the visual containers for the data items are created on a per-need basis.

It's considered a bad practice to acess the visual elements directly but if you really need to do this for one reason or another you should probably turn off the virtualization:

<DataGrid VirtualizingPanel.IsVirtualizing="False" EnableRowVirtualization="False" ... />
          

The other option is otherwise to bind the IsChecked property of the RadioButton to a property of your data object and looking at this property instead of the one of the RadioButton.

CodePudding user response:

Your iteration starts from 0 to your list's count. Seems like the list has 19 elements and your loop continues to the 20th element wich is null.

It should be like this:

for (int i = 0; i < datagrid_wafer_200.Items.Count - 1; i  )
{
    // ...
}  
for (int i = 0; i < datagrid_wafer_300.Items.Count - 1; i  )
{
    // ...
}  
  • Related