How to clear a data grid view

Wizard picture Wizard · Oct 30, 2012 · Viewed 210.4k times · Source

I am trying to populate a DataGridView based on the selected item in a ComboBox, I have this part working.

However, I need to be able to clear the grid before adding the new data from a new item rather than it just adding on to the end.

How do I clear a DataGridView before adding items to it?

Answer

Trevor Pilley picture Trevor Pilley · Oct 30, 2012

Firstly, null the data source:

this.dataGridView.DataSource = null;

Then clear the rows:

this.dataGridView.Rows.Clear();

Then set the data source to the new list:

this.dataGridView.DataSource = this.GetNewValues();