错误:对象引用未设置为对象的实例 - WPF中的datagrid [重复]

时间:2022-07-05 09:06:25

This question already has an answer here:

这个问题在这里已有答案:

I'm new in WPF my app have a simple window with datagrid and a button for read all the cell in it - every time I get Error for null reference but I don't know why?(my grid is Binding and have data)

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

The XAML Code:

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>

and Code behind for binding datagrid

和后面的代码绑定datagrid

        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();
    } 

And my Code for Button and where the error happen is here:

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

        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";
            }
        }
    }

this "(dataGrid1.Items[i] as DataRowView)" is Null and I don't khow why?

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

please help me THX.

请帮帮我THX。

1 个解决方案

#1


0  

Shouldn't your first for loop be like

你的第一个循环不应该像

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

Also, dataGrid1.ItemsSource = LoadCollectionData(); setting the datasource to be List<RowsDatas> and in such case this casting dataGrid1.Items[i] as DataRowView will fail and in-turn will be null. So calling Row on that ((dataGrid1.Items[i] as DataRowView).Row) will result in NRE.

另外,dataGrid1.ItemsSource = LoadCollectionData();将数据源设置为List ,在这种情况下,这个将DataGrid1.Items [i]作为DataRowView的转换将失败,并且转为null。因此调用Row((dataGrid1.Items [i] as DataRowView).Row)将导致NRE。

#1


0  

Shouldn't your first for loop be like

你的第一个循环不应该像

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

Also, dataGrid1.ItemsSource = LoadCollectionData(); setting the datasource to be List<RowsDatas> and in such case this casting dataGrid1.Items[i] as DataRowView will fail and in-turn will be null. So calling Row on that ((dataGrid1.Items[i] as DataRowView).Row) will result in NRE.

另外,dataGrid1.ItemsSource = LoadCollectionData();将数据源设置为List ,在这种情况下,这个将DataGrid1.Items [i]作为DataRowView的转换将失败,并且转为null。因此调用Row((dataGrid1.Items [i] as DataRowView).Row)将导致NRE。