views:

89

answers:

1

I have datagridview on my winform. I am displaying records in the datagridview. Now after displaying the records on the datagridview, I want to remove the row from datagridview which has one or more empy cells that is no value in the cell for that row. So for that I am checking each cell for every row if there is any cell empty or null then I remove that rows using RemoveAt() function.

My code is :

for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                for (int j = 0; j < dataGridView1.Columns.Count; j++)
                {
                    if (string.IsNullOrEmpty(dataGridView1.Rows[i].Cells[j].Value.ToString()))
                    {
                        dataGridView1.Rows.RemoveAt(i);
                        break;
                    }
                }
            }

Then problem is it does not work properly that it does not remove all the rows which has empty cell. So what should I do here ?

+1  A: 

The simplest way to do what you want is to loop through the datagridview rows in reverse. This way your indices stay correct.

for (int i = dataGridView1.Rows.Count -1; i >= 0; i--)
{
    DataGridViewRow dataGridViewRow = dataGridView1.Rows[i];

    foreach (DataGridViewCell cell in dataGridViewRow.Cells)
    {
        string val = cell.Value as string;
        if (string.IsNullOrEmpty(val))
        {
            if (!dataGridViewRow.IsNewRow)
            {
                dataGridView1.Rows.Remove(dataGridViewRow);
                break;
            }
        }            
    }
}

I'm doing a couple of extra things that you may not need to do (the code is just cut and pasted from my test application)

  • I usually grab the row in question into a DataGridViewRow object.
  • I am checking the IsNewRow property because my grid was editable.
  • I am assigning the value to a string variable (with an as to cast it) since the way you had it was throwing an exception for me.
David Hall
@David Hall,But sir this removes all the row.
Harikrishna
@Harikrishna - the code above doesn't check through the cells for empty ones but the code you already had will do that. I've just demonstrated how to deal with the changing indexes as you remove your rows.
David Hall
This latest edit has code to check for the empty cells. You will likely hit issues, around different column types like checkbox or comboboxes, but you will need to work through those.
David Hall
@David Hall, What is happened that while checking reverse is `dataGridViewRow.Cells[0].Value as string;` is every time null.So it removes all rows. I don't understand why first cell of the each row is null ?
Harikrishna
@Harikrishna without more info I can't really debug this for you. What is the type of the 0th column? Is it empty? And do you perhaps have hidden columns? Note also how I'm using a foreach over the cells, and have removed the index reference. (I also had a bug that I've fixed, where I was using continue rather than break)
David Hall
@David Hall,Ok..Sir it's done..Thank You Very Much..I am updating my question with that code,please check it whether it is ok or not.
Harikrishna
@Harikrishna The code looks ok to me (though the way I wrote it is closer to my preferred style). I can't really answer whether the answer works or not in your situation (if it does work for you, you know what to do :) )
David Hall
@David Hall,Sir I have another problem that is I am sorting the datatable datewise then I am displaying that datatable in the datagridview like `dataGridView1.DataSource = TableWithOnlyFixedColumns.DefaultView;` but it is now synchornization of datatable and datagridview So if datatable changes suddenlly datagridview is changed what I don't want.And if I copy the data of datatable cell by cell to datagridview then record is sorted in datagridview. What should I do for that ?
Harikrishna
@David Hall,Sir I have used your code at all,just I edit only one thing that is checking each cell.
Harikrishna
@Harikrishna That's really another question. If I was to start really going into this new issue it would clutter this question. One suggestion though would be to not bind directly to the datatable, instead bind to something else that won't be updated. (though, why don't you want users to see the updates?)
David Hall