DataGridView.Rows.RemoveAt(intindex)不会删除Windows窗体中的行?inti=dataGridView1.CurrentRow.Index;dataGridView1.Rows.RemoveAt(i);上面的代码用于删除当前选定的行(只允许选择1行),但我仍然可以看到该行保留在datagridview中。我错过了更新还是无效?ps:dataGridView1是数据绑定的。ps2:我用linq查询绑定的数据,我不想再次查询只是为了显示一行被删除,我认为这会降低性能。我认为解决方案取决于您如何绑定数据。如果您正在绑定BindingList,则RemoveAt()方法会删除所有内容而无需刷新DataGridView。试试下面看看...dataGridView1.Refresh();或者重新绑定数据看看..dataGridView1.DataSource=myDataSource;dataGridView1.Refresh();从数据源中删除项目并再次绑定。由于数据来自linq,在绑定之前,你可以这样做:varresult=fooLinq.ToList();dataGridView1.DataSource=结果;//删除并再次绑定varresult=dataGridView1.DataSourceasList;结果.RemoveAt(fooIndex);dataGridView1.DataSource=结果;如果我理解正确,你想从DGV中删除用户选择的行。使用DGV的DataGridViewRowCollection而不是DataTable的DataRowCollection。DataGridViewRow有一个Selected属性,它指示该行是否被选中。一旦确定要删除该行,就可以使用DataGridViewRowCollection的Remove方法从网格中删除该项目,例如YerDataGridView.Rows.Remove(row)请注意,此时,虽然该项目已从DGV中删除,但它已尚未从AccessDB中的“已删除”中删除。您需要调用DataSet/DataTable上的TableAdapterUpdate方法以将删除提交到数据库,例如YerTableAdapter.Update(YerDataSet)我通常在删除所有要从DGV中删除的项目后调用更新一次以提交更改。以上就是C#学习教程:DataGridView.Rows.RemoveAt(intindex)不会删除WindowsForm中的行?如果所有分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
