C# DataGridView.Rows.RemoveAt(int index)不删除windows窗体中的行吗?

C# DataGridView.Rows.RemoveAt(int index)不删除windows窗体中的行吗?,c#,.net,winforms,data-binding,row,C#,.net,Winforms,Data Binding,Row,上面删除当前选中行的代码只允许选择1行,但我仍然可以看到该行仍保留在datagridview中。我是否错过了更新或使其无效 ps:dataGridView1是数据绑定的 ps2:我使用linq query绑定数据,我不想再次查询,因为只是为了显示一行被删除,我认为这会降低性能。我认为解决方案取决于绑定数据的方式。如果您正在绑定BindingList,RemoveAt方法将为您删除所有处理,并且您不必刷新DataGridView 试试下面,看看 int i = dataGridView1.Curr

上面删除当前选中行的代码只允许选择1行,但我仍然可以看到该行仍保留在datagridview中。我是否错过了更新或使其无效

ps:dataGridView1是数据绑定的


ps2:我使用linq query绑定数据,我不想再次查询,因为只是为了显示一行被删除,我认为这会降低性能。

我认为解决方案取决于绑定数据的方式。如果您正在绑定BindingList,RemoveAt方法将为您删除所有处理,并且您不必刷新DataGridView

试试下面,看看

int i = dataGridView1.CurrentRow.Index;
dataGridView1.Rows.RemoveAt(i);
或者重新绑定数据并查看

dataGridView1.Refresh();

从数据源中删除该项,然后再次绑定

由于数据来自linq,因此在绑定之前,您可以执行以下操作:

dataGridView1.DataSource = myDataSource;
dataGridView1.Refresh();

如果我理解正确,您希望从DGV中删除用户选择的行

使用DGV的DataGridViewRowCollection,而不是DataTable的DataRowCollection。DataGridViewRow具有Selected属性,该属性指示是否选中某一行

确定要删除行后,可以使用DataGridViewRowCollection的Remove方法从网格中删除该项,例如YerDataGridView.Rows.Removerow

请注意,此时,尽管该项已从DGV中删除,但尚未从Access DB中删除。您需要在DataSet/DataTable上调用TableAdapter更新方法,以将删除提交给数据库,例如YerTableAdapter.UpdateYerDataSet


我通常只在从DGV中删除所有要删除的项目后才调用Update提交更改。

谢谢,但该链接并不能解决我的问题。我通过阅读以下内容解决了这个问题:感谢您的所有建议。我尝试了DGV.Refresh;和dgv.Parent.Refresh;但事实并非如此help@Sun:能否显示如何绑定数据?dataGridView1.DataSource=myDataSource;请看…我使用linq query绑定数据,我不想再次查询只是为了显示行已删除,我认为这会降低性能。为什么不能从datasouce中删除它并刷新dataGridView?我使用linq query绑定数据,我不想再次查询只是为了显示行已删除,我认为这会降低性能。您不需要再次查询,我提供了更多详细信息…FooType是您从linq返回的实际业务类型…是的,我知道使用此方法时不会从DB中删除该项目
var result = fooLinq.ToList();
dataGridView1.DataSource = result;


//to remove and bind again
var result = dataGridView1.DataSource as List<FooType>;
result.RemoveAt(fooIndex);
dataGridView1.DataSource = result;