如何删除DataGridView的所有行?

问题描述 投票:0回答:2

这是代码:

         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 
    }

错误发生在这里如何删除DataGridView的所有行?我的DataGridView是BindingSource

c# datagridview delete-row
2个回答
6
投票

您可以将DataGridView DataSource设置为null而不是清除行。

替换此:

datagridview1.Rows.Clear();

有以下内容:

datagridview1.DataSource=null;

0
投票
while (dataGridView1.Rows.Count>0)
        {
            dataGridView1.Rows.Remove(dataGridView1.Rows[0]);
        }
© www.soinside.com 2019 - 2024. All rights reserved.