C# 如何删除DataGridView中的所有行?
这是代码:C# 如何删除DataGridView中的所有行?,c#,datagridview,delete-row,C#,Datagridview,Delete Row,这是代码: BindingSource bs = new BindingSource(); DataTable tbl(string sql) { OracleConnection con = new OracleConnection(connectionstring); OracleDataAdapter adp = new OracleDataAdapter(sql, con); DataS
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中的所有行?
my DataGridView是BindingSource您可以将
DataGridView
DataSource
设置为null
,而不是清除行
替换此项:
datagridview1.Rows.Clear();
包括以下内容:
datagridview1.DataSource=null;
您可以将
DataGridView
DataSource
设置为null
,而不是清除行
替换此项:
datagridview1.Rows.Clear();
包括以下内容:
datagridview1.DataSource=null;
谢谢Man@SudhakarYou Welcome:)我很高兴能帮助你。我不确定为什么需要
datagridview1.Rows.Clear()
是的,你是对的,我正在清除这些行,只要数据源设置为null,Gridview就会变为空。感谢您的宝贵意见,我已对我的答案进行了更改,谢谢。如果在designview中使用“bindingsource”设置Datagridview,则此操作不起作用。您必须直接处理bindingsource。然后清除datatable,或者需要清除的任何内容Dim bs As BindingSource=DirectCast(dgvGrid1.DataSource,BindingSource)Dim ds As DataSet=DirectCast(bs.DataSource,DataSet)Dim dt As DataTable=ds.Tables(0)dt.Rows.Clear()
Thank Man@SudhakarYou Welcome:)我很高兴帮助你。我不确定为什么datagridview1.Rows.Clear()
is required是的,您是对的,我正在清除行的上下文中,一旦DataSource设置为null,Gridview就会变为空。感谢您的宝贵意见,我已对我的答案进行了更改,谢谢。如果在designview中使用“bindingsource”设置Datagridview,则此操作不起作用。您必须直接处理bindingsource。然后清除datatable,或者需要清除的任何内容Dim bs As BindingSource=DirectCast(dgvGrid1.DataSource,BindingSource)Dim ds As DataSet=DirectCast(bs.DataSource,DataSet)Dim dt As DataTable=ds.Tables(0)dt.Rows.Clear()