0

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)

The XAML Code:

<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

        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:

        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?

please help me THX.

  • By `dataGrid1.ItemsSource = LoadCollectionData();` you assign the ItemsSource property to a collection of `RowsDatas`. Quite obviously then the items aren't of type `DataRowView`. That's why `dataGrid1.Items[i] as DataRowView` returns null. – Clemens Jul 10 '16 at 13:59
  • 1
    You fill the DataGrid with a List and try to read a row as it is a DataRowView. Of course this is a null and any code after that is a nullreferenceexception – Steve Jul 10 '16 at 13:59
  • So sorry for my duplicate and Beginner question and thankful for your comments and answers - but one question how can I change my "RowsDatas" or "as DataRowView" to reach Cell values in this datagrid? Thx – Saeed Azarafrooz Jul 10 '16 at 14:21

1 Answers1

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.

Rahul
  • 71,392
  • 13
  • 57
  • 105