Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear a data grid view

Tags:

c#

datagrid

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?

like image 675
Wizard Avatar asked Oct 30 '12 11:10

Wizard


People also ask

How do you edit data grid view?

By default, users can edit the contents of the current DataGridView text box cell by typing in it or pressing F2.

How do I hide data grid view?

Set the DataGridViewColumn. Visible property to false .

What is DataGridView in C#?

The DataGridView control provides a customizable table for displaying data. The DataGridView class allows customization of cells, rows, columns, and borders through the use of properties such as DefaultCellStyle, ColumnHeadersDefaultCellStyle, CellBorderStyle, and GridColor.


1 Answers

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(); 
like image 56
Trevor Pilley Avatar answered Sep 26 '22 05:09

Trevor Pilley