Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
How can I remove /delete empty row of a datagridview?
Posted
Updated 27-Jul-22 17:54pm
v2
Comments
TweakBird 17-Jan-11 6:46am    
can you share your code(what you have tried).
Dalek Dave 17-Jan-11 7:33am    
Minor Edit for Grammar and Spelling.

now I find a neat solution:

yourDataGridView.AllowUserToAddRows = false;
 
Share this answer
 
Comments
Kats2512 28-Jul-22 3:54am    
This has nothing to do with the question.
Southmountain 7-Aug-22 23:28pm    
the empty row is designed behavior for Windows designer.
so this statement will remove the last empty row.

you can test what I said....
I gess this could help you ,

bool Empty = true;

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
    Empty = true;
    for (int j = 0; j < dataGridView1.Columns.Count; j++)
    {
        if (dataGridView1.Rows[i].Cells[j].Value != null && dataGridView1.Rows[i].Cells[j].Value.ToString() != "")
        {
            Empty = false;
            break;
        }
    }
    if (Empty)
    {
        dataGridView1.Rows.RemoveAt(i);
    }
}
 
Share this answer
 
v2
Take a look at there[^] same Question having solution given below

C#
for (int i = 1; i < dataGridView1.RowCount - 1; i++)
 {
 if (dataGridView1.Rows[i].Cells[0].Value.ToString() == "")
 {
       dataGridView1.Rows.RemoveAt(i);
       i--;
 }
 }
 
Share this answer
 
v3
Comments
Hiren solanki 17-Jan-11 7:05am    
That just checks for if row having two cells or static cells , what if you don't know about cells in a row ?
RaviRanjanKr 17-Jan-11 7:08am    
OP Question was not too much clear that's why I tried to give this answer.
Thanks for Inform me
Now Answer is updated..
Adhitya Ganesan 4-May-17 6:58am    
Actually, the loop should start from i=0......but a beautiful snippet nevertheless....thanks
Very useful thank you very much
Check with the source before binding to datagridview

Like if you're using DataTable to bind datagridview then you can use following procedure.

C#
foreach(DataRow row in dt.Rows)
       {
           bool IsEmpty = false;
           foreach (object obj in row.ItemArray)
           {
               if (String.IsNullOrEmpty(obj.ToString()))
               {
                   IsEmpty = true;
               }
               else
               {
                   IsEmpty = false;
               }
           }
           if (IsEmpty)
           {
               dt.Rows.Remove(row);
           }
}


Thanks.
 
Share this answer
 
Comments
sevenbell 17-Jan-11 7:03am    
in which event i use this code
Hiren solanki 17-Jan-11 7:04am    
Before you bind data to gridview you should check for empty lines in source.

In short before Gridview.DataBind(); just check all the rows.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900