Click here to Skip to main content
15,569,407 members
Please Sign up or sign in to vote.
1.06/5 (4 votes)
See more:
how to clear datagridview in c#
Posted
Updated 5 days ago

Use This Code When U want to clear

C#
gridView1.DataSource = null;
gridView1.DataBind();
 
Share this answer
 
You don't clear the grid, you clear the data

C#
DataGridView.DataSource = null;
 
Share this answer
 
Comments
Member 3850876 1-Oct-19 1:14am    
thanks
Try this :

dataGridView1.Rows.Clear()

OR
dt.Rows.Clear() // If dgv is bound to datatable
dataGridView1.DataBind();


OR

Use this code to check if you are bound to a data source :

//Code Block
if (this.dataGridView1.DataSource != null)
{
     this.dataGridView1.DataSource = null;
}
else
{
    this.dataGridView1.Rows.Clear();
}
 
Share this answer
 
v2
C#
this.dataGridView1.Rows.Clear();

removes rows. In my case I dave a DGV with 5 rows, after
C#
this.dataGridView1.Rows.Clear();

I found my DGV has only 1 row.
I tried
C#
this.dataGridView1.Columns.Clear();
with the same results - number of columns was changed. So what I am doing now is a loop with manually moving "" to each cell.
 
Share this answer
 
Hi Every One I have tried to reload the datagridview rows at every time i click the button. but the problem is we can't reload the datagridview in windows forms like gridview in asp in asp for every button click event the page will be reloaded so the grid will updated with values but in windows forms it will not happen so we have to clear for clear we can't use

datagridview1.Rows.Clear()

it will raise the error so, i search for solutions in many websites but i can't get it at that time i got this below idea using this we can remove our rows in grid and we can rebind it with our new values

if (datagridview1.RowCount > 0)
{
for (int i = 0; i <= datagridview1.RowCount; i++)
{
datagridview1
C#

.Rows.RemoveAt(0);
}
}
 
Share this answer
 
U can use this code to solve your problem.
C#
DataGridView.DataSource = null;

AND
C#
dataGridView1.Rows.Clear()


Thanks & regard
Sham
 
Share this answer
 
Comments
CHill60 15-Jun-13 10:13am    
That'll be the same as Solution 3 that was posted over a year ago
Try this:
C#
dataGridView1.Rows.Clear(); // clear rows form dataGridView
dataGridView1.Refresh(); // refresh dataGridView
 
Share this answer
 
Comments
CHill60 15-Jun-13 10:14am    
The question is over a year old and has already been resolved
C#
foreach (Control field in container.Controls)
{
    if (field is TextBox)
        ((TextBox)field).Clear();
    else if (field is ComboBox)
        ((ComboBox)field).SelectedIndex=0;
    else
        dgView.DataSource = null;
        ClearAllText(field);
}
 
Share this answer
 
Comments
Richard MacCutchan 15-Jul-15 3:41am    
This question is more than three years old. Please do not post in dead questions.
Sergey Alexandrovich Kryukov 15-Jul-15 8:55am    
The problem is different, it's worse. Please see our comments here:
http://www.codeproject.com/Answers/1009732/code-for-clearing-all-textbox-in-a-form-csharp?arn=0#answer6.

The member account is reported for abuse twice, at the moment of writing. Next time, it can be reported to the forum.

—SA
Try this:

dataGridView1.Columns.Clear()
 
Share this answer
 
Comments
CHill60 5 days ago    
Mentioned is so many previous solutions, some dating back over 10 years. If you are going to answer old posts then bring something new to the thread

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