-2

I'm facing an issue when trying to verify if a cell of a wpf datagrid is null, I always get a null reference exception even when I try to verify if it's null, can anyone help me here?

code bellow

for (int i = 0; i < commandeDataGrid.Items.Count; i++)
        {

            DataRowView row = commandeDataGrid.Items[i] as DataRowView;

            if (row["Prix Total TTC"]!=null)
            {
               count = count + Convert.ToInt16(row["Prix Total TTC"]);
            }

        }
Saber CHETIOUI
  • 50
  • 1
  • 13
  • Where do you get the Null Ref Exception? Have you looked in a debugger to see what is null? I don't see you trying to "to verify if it's null" anywhere in the code you posted. Do you know about the "`?.`" and "`??`" operators? – Flydog57 Nov 02 '18 at 22:01
  • as you can see I verified if it's not null in order to excute a function, but the debugger display a null reference exception in the condition " if (row["Prix Total TTC"]!=null)" – Saber CHETIOUI Nov 02 '18 at 22:07
  • Sorry, I missed that. My guess is that `row` is null. If you put a breakpoint on the `if` statement, you should see that `row` is null. The result is that when you try to access the `"Prix Total TTC"` column, you'll get the Null Ref exception. Make the `if` test the row's null-ness, and the change the count line to: `count = count + Convert.ToInt16(row["Prix Total TTC"] ?? "0");` You might also consider using a full `int` rather than a 16-bit short for `count`, and consider using `int.TryParse` rather than `Convert.ToInt` – Flydog57 Nov 02 '18 at 22:29
  • it still did'nt work, are you sure about (row["Prix Total TTC"] ?? "0") because "0" is not a null value, here is exactly what I want to do, I have a datagrid with a cell editing change even handler inside which there is the bottom code, when I am changing the cells value a label gets automatically the total sum of the "Prix Total TTC" cell – Saber CHETIOUI Nov 02 '18 at 22:42
  • 1
    OK, what "didn't work"? Am I sure about `(row["Prix Total TTC"] ?? "0")`? No! I don't know enough about your code. What that says is "look at the "Prix Total TTC" cell, if for some reason it's null, consider it to be the string "0". The important thing for you to fix is to change `if (row["Prix Total TTC"]!=null)` to `if (row != null)`. My educated guess is that `row` is null. But, you have the code in front of you, and you have a debugger. You should be able to step through your code and see what's null. I can't do that from here. Bonne Chance! – Flydog57 Nov 02 '18 at 23:28

1 Answers1

1

You should check whether the as operator actually returns a DataRowView:

for (int i = 0; i < commandeDataGrid.Items.Count; i++)
{
    DataRowView row = commandeDataGrid.Items[i] as DataRowView;
    if (row != null && row["Prix Total TTC"] != null)
    {
        count = count + Convert.ToInt16(row["Prix Total TTC"]);
    }
}

Or better yet iterate through the ItemsSource:

DataView dataView = commandeDataGrid.ItemsSource as DataView;
if (dataView != null)
{
    foreach (DataRowView row in dataView)
    {
        if (row["Prix Total TTC"] != null)
        {
            count = count + Convert.ToInt16(row["Prix Total TTC"]);
        }
    }
}
mm8
  • 163,881
  • 10
  • 57
  • 88