Search code examples
c#winformsdatagridview

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;

Solution

  • set datasource as null

    dataGridView1.DataSource = null;
    

    Or

    dataGridView1.Rows.Clear()
    

    OR

    while (dataGridView1.Rows.Count > 0)
    {
        dataGridView1.Rows.RemoveAt(0);
    }