Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0
how to clear datagridview in c#
Posted 20-Feb-12 1:21am
spnpra644
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Use This Code When U want to clear
 
gridView1.DataSource = null;
gridView1.DataBind();
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You don't clear the grid, you clear the data
 
DataGridView.DataSource = null;
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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();
}
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

this.dataGridView1.Rows.Clear();
removes rows. In my case I dave a DGV with 5 rows, after
this.dataGridView1.Rows.Clear();
I found my DGV has only 1 row.
I tried
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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

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
.Rows.RemoveAt(0);
}
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

U can use this code to solve your problem.
DataGridView.DataSource = null;
AND
dataGridView1.Rows.Clear()
 
Thanks & regard
Sham
  Permalink  
Comments
CHill60 at 15-Jun-13 10:13am
   
That'll be the same as Solution 3 that was posted over a year ago
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 9

Try this:
dataGridView1.Rows.Clear(); // clear rows form dataGridView
dataGridView1.Refresh(); // refresh dataGridView
  Permalink  
Comments
CHill60 at 15-Jun-13 10:14am
   
The question is over a year old and has already been resolved

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



Advertise | Privacy | Mobile
Web01 | 2.8.141022.2 | Last Updated 15 Jun 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100