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
  • 115,091
  • 17
  • 196
  • 297
  • 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
  • 87,323
  • 22
  • 191
  • 272
Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • -1 This is still a problem – Sriram Sakthivel Jan 16 '14 at 08:11
  • Why would you subtract 1? Why not just use < instead of <= and avoid having to subtract 1? – Zinoex Jan 16 '14 at 08:12
  • @SriramSakthivel, It will solve the most *obvious* problem with that design. Why `NullReferenceException` occurs - nobody knows yet. – Sinatr Jan 16 '14 at 08:13
  • @Highace2, it doesn't matter, i just take OP code. – Sinatr Jan 16 '14 at 08:14
  • I have already done that for precautions. But it is not the real problem, the problem is that the String layDate is being passed by a null reference, and I want to know how to avoid that. I already used exceptions adn it works but leaves one value left behind which matches the criteria. – user3167860 Jan 16 '14 at 08:14
  • @Sinatr 1) This will still give terrible result, `dataGridViewLay.Rows.RemoveAt(i);` re arranges the rows, 2)`i <= dataGridViewLay.RowCount-1` is second problem, 3) doesn't answers the question – Sriram Sakthivel Jan 16 '14 at 08:19
  • @SriramSakthivel, you are fast at posting, care to read my edit/comments? – Sinatr Jan 16 '14 at 08:20
  • @Sinatr I removed my downvote, you addressed my point 3, but not 1 and 2. – Sriram Sakthivel Jan 16 '14 at 08:22
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,230
  • 1
  • 10
  • 21
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,870
  • 14
  • 54
  • 83
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.

Zinoex
  • 512
  • 2
  • 8
  • 22