Search code examples
c#datagridviewdelete-row

How do delete All rows at DataGridView?


this is code :

         BindingSource bs = new BindingSource();


       DataTable tbl(string sql)
        {

        OracleConnection con = new OracleConnection(connectionstring);
        OracleDataAdapter adp = new OracleDataAdapter(sql, con);
        DataSet ds = new DataSet();
        adp.Fill(ds, "tbl");
        return ds.Tables["tbl"];
        }
 void GetData()
  {

   bs.DataSource = Class1.tbl("select USER_ID   ,EMP_NAME as pName,EMP_MOBILE from TBLUSERS");
            datagridview1.Columns[0].DataPropertyName = "USER_ID";
            datagridview1.Columns[1].DataPropertyName = "pName";
            datagridview1.Columns[2].DataPropertyName = "EMP_MOBILE";
            datagridview1.DataSource = bs;
 }

  void ClearAllRows()
    {
          datagridview1.Rows.Clear();
   //The error occurs here 
    }

The error occurs here How do delete All rows at DataGridView ? my DataGridView is BindingSource


Solution

  • you can set your DataGridView DataSource to null instead of clearing the Rows.

    Replace this :

    datagridview1.Rows.Clear();
    

    With Following:

    datagridview1.DataSource=null;