错误:对象引用未设置为 WPF 中的对象实例 - 数据网格

本文关键字:实例 对象 数据 网格 数据网 对象引用 设置 WPF 错误 | 更新日期: 2023-09-27 17:56:08

我是WPF的新手,我的应用程序有一个简单的窗口,带有数据网格和一个用于读取其中所有单元格的按钮 - 每次我收到空引用错误时,但我不知道为什么?我的网格是绑定的并且有数据)

XAML 代码:

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <DataGrid  AutoGenerateColumns="False" Height="232" HorizontalAlignment="Left" Margin="12,67,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="479" Loaded="dataGrid1_Loaded">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Sheba}" Header="شماره شبا" MinWidth="200"> </DataGridTextColumn>
        <DataGridTextColumn Width="*" Binding="{Binding TrueFalse}" Header="صحت شماره شبا" > </DataGridTextColumn>
        <DataGridTextColumn Width="*" Binding="{Binding Country}" Header="کشور"> </DataGridTextColumn>
        <DataGridTextColumn Width="*" Binding="{Binding BankName}" Header="نام بانک">  </DataGridTextColumn>
    </DataGrid.Columns>
    </DataGrid>
    <Button Content="Button" Height="25" HorizontalAlignment="Left" Margin="36,26,0,0" Name="button1" VerticalAlignment="Top" Width="85" Click="button1_Click" />
    <Button Content="remove" Height="21" HorizontalAlignment="Left" Margin="136,32,0,0" Name="button2" VerticalAlignment="Top" Width="79" Click="button2_Click" />
</Grid>

和用于绑定数据网格的代码隐藏

        public class RowsDatas
    {
        public string Sheba { get; set; }
        public string TrueFalse { get; set; }
        public string Country { get; set; }
        public string BankName { get; set; }
    }
    private List<RowsDatas> LoadCollectionData()
    {
        List<RowsDatas> authors = new List<RowsDatas>();
        for (int i = 0; i < 10; i++)
        {
            authors.Add(new RowsDatas()
            {
                Sheba = "111111",
                TrueFalse = "TrueFalse",
                Country = "5555",
                BankName = "Bank",
            });
        }
        return authors;
    }
    private void dataGrid1_Loaded(object sender, RoutedEventArgs e)
    {
        dataGrid1.ItemsSource = LoadCollectionData();
    } 

我的按钮代码和错误发生的地方在这里:

        private void button1_Click(object sender, RoutedEventArgs e)
    {
        string sHeaders = "";
        string stOutput = "";
        for (int j = 0; j < dataGrid1.Columns.Count; j++)
            sHeaders = sHeaders.ToString() + Convert.ToString(dataGrid1.Columns[j].Header) + "'t";
        stOutput += sHeaders + "'r'n";
        for (int i = 0; i < dataGrid1.Items.Count - 1; i++)
        {
            string stLine = "";
            for (int j = 0; j < dataGrid1.Columns.Count - 1; j++)
            {
**//Error: Object reference not set to an instance of an object.**
                string a = (dataGrid1.Items[i] as DataRowView).Row.ItemArray[j].ToString(); 
                stLine = stLine.ToString() + "'t";
                stOutput += stLine + "'r'n";
            }
        }
    }

这个"(dataGrid1.Items[i] as DataRowView)"是空的,我不知道为什么?

请帮帮我谢谢。

错误:对象引用未设置为 WPF 中的对象实例 - 数据网格

你的第一个for循环不应该像

    for (int i = 0; i < dataGrid1.Rows.Count; i++)
    {

此外,dataGrid1.ItemsSource = LoadCollectionData();将数据源设置为List<RowsDatas>,在这种情况下,此转换dataGrid1.Items[i] as DataRowView将失败,反过来将null。因此,调用Row(dataGrid1.Items[i] as DataRowView).Row)将导致NRE。