C# 如何删除 DataGridView 中的所有行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19958704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:13:46 来源:igfitidea点击:
How do delete All rows at DataGridView?
提问by ???? ???????
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
此处发生错误 How do delete All rows at DataGridView ?我的 DataGridView 是 BindingSource
采纳答案by Sudhakar Tillapudi
you can set your DataGridView
DataSource
to null
instead of clearing the Rows.
您可以将您设置DataGridView
DataSource
为null
而不是清除行。
Replace this :
替换这个:
datagridview1.Rows.Clear();
With Following:
随着以下:
datagridview1.DataSource=null;
回答by Saleem Kalro
while (dataGridView1.Rows.Count>0)
{
dataGridView1.Rows.Remove(dataGridView1.Rows[0]);
}