user1727804
user1727804

Reputation: 21

How to remove empty row from gridview

I have a problem in window application. When I insert a record and display the records in a gridview, the gridview automatically makes one empty row. And I also use

dataGridView1.AllowUserToAddRows = false;

Please help me with an alternative solution to get rid of the empty row.

Upvotes: 2

Views: 8817

Answers (2)

Thinhbk
Thinhbk

Reputation: 2214

Default DGV will have a blank row at the bottom to enable user adding a new row, by setting dataGridView1.AllowUserToAddRows = false; will disable the blank row.

You may delete the blank rows manually like this:

for (int i = 1; i < dataGridView1.RowCount - 1; i++)    
{  
   Boolean isEmpty = true;
   for(int j=0;j<dataGridView1.Columns.Count; j++)
   {
     if (dataGridView1.Rows[i].Cells[j].Value.ToString() != "" )    
      { 
          isEmpty = false;
          break;
      }
    }
    if (isEmpty)    
    {    
       dataGridView1.Rows.RemoveAt(i);
       i--;
    }   
 }

HTH.

Upvotes: 4

jgok222
jgok222

Reputation: 414

Are you sure you don't have a blank record in your datasource? another way you could check this is to add an if statment on to the OnRowDataBound to check the item index which if 0 (or some other way to identify the blank row) then set the row's Visible = false

Upvotes: 0

Related Questions