Search code examples
c#datagrid

How to clear a data grid view


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?


Solution

  • 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();