Praveen Kumar
Praveen Kumar

Reputation: 43

How to clear DataGridView in C# windows forms?

I used MS-Access to fill DataGridView in my C# Windows Form Application. When i query datas from Access database it displays correctly. But when i press the view data button again it adds empty columns. This process repeats. I used

dataGridView1.DataSource = null;
dataGridView1.Rows.Clear();
dataGridView1.Refresh();

to clear DataGridView but no luck.

Upvotes: 0

Views: 22163

Answers (2)

user8128167
user8128167

Reputation: 7696

You can also clear what the DataSource is bound to or do the following:

dataGridView1.DataSource = null;
dataGridView1.DataBind();

See https://www.codeproject.com/Questions/332902/how-to-clear-datagridview-in-csharp

Or,

https://social.msdn.microsoft.com/Forums/windows/en-US/ca891be5-4f5c-4c70-9123-6cbf7a97c93b/clearing-a-datagridview?forum=winformsdatacontrols

Upvotes: 0

Equalsk
Equalsk

Reputation: 8224

From the comments on the original question:

Try clearing the columns as well

dataGridView1.Columns.Clear();

Upvotes: 6

Related Questions