Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to empty a datagridview?

I don't know what is the syntax for emptying a datagridview. Please help me here is my code.

if (cboProduct.SelectedIndex != -1)
    load_variant();
else
    //empty the datagridview
    cboProduct.SelectedIndex = -1;
like image 790
Harvey Avatar asked May 14 '13 06:05

Harvey


3 Answers

set datasource as null

dataGridView1.DataSource = null;

Or

dataGridView1.Rows.Clear()

OR

while (dataGridView1.Rows.Count > 0)
{
    dataGridView1.Rows.RemoveAt(0);
}
like image 153
Damith Avatar answered Sep 19 '22 01:09

Damith


syntax for emptying a datagridview

Just assign null to its DataSource property.

yourGridView.DataSource = null;
like image 45
Habib Avatar answered Sep 23 '22 01:09

Habib


Just set DataGridView.DataSource property to null

Gets or sets the data source that the DataGridView is displaying data for.

DataGridView1.DataSource = null;

As an alternative (not exactly what .DataSource = null does)

DataTable dt = (DataTable)DataGridView1.DataSource;
if(dt != null)
    dt.Clear();
like image 35
Soner Gönül Avatar answered Sep 22 '22 01:09

Soner Gönül