0

这是代码:

         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

4

3 回答 3

8

您可以设置DataGridView DataSourcenull 而不是清除行。

替换这个:

datagridview1.Rows.Clear();

与以下:

datagridview1.DataSource=null;
于 2013-11-13T16:25:37.943 回答
0

请执行此代码

if(dgvItemDetails.Rows.Count>0) {
    do
    {
        dgvItemDetails.Rows.RemoveAt(0);
    }
    while (dgvItemDetails.Rows.Count > 0);
}
于 2022-02-12T09:41:29.680 回答
0
while (dataGridView1.Rows.Count>0)
{
    dataGridView1.Rows.Remove(dataGridView1.Rows[0]);
}
于 2020-03-21T10:42:50.937 回答