0
 int rowCount = dataGridViewLay.RowCount-1;
       // int columnCount = dataGridViewLay.ColumnCount;

        MessageBox.Show(rowCount+" ");


            for (int i = 0; i <= rowCount; i++)
            {

                String layDate = dataGridViewLay.Rows[i].Cells[1].Value.ToString();
                    if (!layDate.Equals(layDatePicker.Value.ToShortDateString()))
                    {

                       // MessageBox.Show("Match Found");

                         dataGridViewLay.Rows.RemoveAt(i);
                    }
            }

I used the above code, but there popped up an exception; a NullReferenceException and I cannot understand the reason for that anybody have an idea about this?

leppie
  • 109,129
  • 16
  • 185
  • 292
  • On which line does the exception happen? – Plue Jan 16 '14 at 08:09
  • at the "String layDate= dataGridViewLay.Rows[i].Cells[1].Value.ToString();" line – user3167860 Jan 16 '14 at 08:11
  • So make sure that your rows have cells before accessing it – Plue Jan 16 '14 at 08:16
  • I would have look at the line `dataGridViewLay.Rows[i].Cells[1].Value`. Are you sure that the the cell `Cells[1]` is populated? – Alberto Solano Jan 16 '14 at 08:17
  • If the problem is line with value then debug. Check what is actually null there. dataGridViewLay.Rows[i]? dataGridViewLay.Rows[i].Cells[1]? dataGridViewLay.Rows[i].Cells[1].Value? check why. If this is a correct situation add a check for null value. – ElDog Jan 16 '14 at 08:22

4 Answers4

0

You should not cache the length:

for (int i = 0; i <= dataGridViewLay.RowCount-1; i++)
{
...
}

Regarding NullReferenceException: layDate may become null if in any Row you have Cell[1] null. And then null.ToString() is an exception.

This will work

for (int i = 0; i <= dataGridViewLay.RowCount-1; i++)
    if(somecondition)
        dataGridViewLay.Rows.RemoveAt(i);

and it's equal to

for (int i = 0; i < dataGridViewLay.RowCount; i++)
    if(somecondition)
        dataGridViewLay.Rows.RemoveAt(i);

in a way what you get same result. It's not optimal, but it works and is not a mistake.

Flexo
  • 82,006
  • 22
  • 174
  • 256
Sinatr
  • 18,856
  • 9
  • 75
  • 248
0

After you delete the first row the number of rows decrease while your count is still the same. You should iterate from the end.

for (int i = dataGridViewLay.RowCount-1; i >= 0; i--)
ElDog
  • 1,190
  • 9
  • 19
0

You Referencing Row of DatagridView through i and your i refering a row which is not in DataGridView

On

dataGridViewLay.Rows.RemoveAt(i);

It Points to a row which is not present in grid or it did'nt Get a referance of row which have to remove from Datagridview thus you got Null Reference exception
So try to find out at which row you getting this exeption

For more Detail Null Refernce Exception

Try this

foreach( DataGridViewRow dgr in dataGridView1.Rows.Cast<DataGridViewRow>().Where(r => r != null).ToList())
 { 
   //remove Those rows Which is not equal to null             
 }
Amit Bisht
  • 4,355
  • 13
  • 47
  • 80
0

It's is just a suggestion but when you find the layDate thingy then you could break out of the loop (the breakkeyword). This would avoid the number of rows not being the same as your cached row count.

Highace2
  • 504
  • 2
  • 7
  • 22