DataGridView.Rows.RemoveAt(int index) 不会删除 Windows 窗体中的行?
int i = dataGridView1.CurrentRow.Index;
dataGridView1.Rows.RemoveAt(i);
上面的代码用于删除当前选定的行(仅允许选择 1 行),但我仍然可以看到该行保留在 datagridview 中。我是否错过了更新或失效?
ps:dataGridView1 是数据绑定的。
ps2:我使用 linq 查询来绑定数据,我不想再次查询只是为了显示一行被删除,我认为这会降低性能。
int i = dataGridView1.CurrentRow.Index;
dataGridView1.Rows.RemoveAt(i);
The above code for deleting current selected row (only allow to select 1 row) but I still can see the row remains in datagridview. Did I miss an update or invalidate ?
ps: dataGridView1 is data-bound.
ps2: I use linq query to bind data and I don't want to query again for just to show a row was deleted, I think it will slow down performance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为解决方案取决于您如何绑定数据。如果您要绑定 BindingList,RemoveAt() 方法会为您删除处理所有内容,并且您不必刷新 DataGridView。
尝试下面并查看...
或重新绑定数据并查看..
I think the solution depends on how you bind the data. If you are binding a BindingList, RemoveAt() method removes handles everything for you and you don't have to refresh the DataGridView.
Try below and see...
OR re-bind the data and see..
从数据源中删除该项目,然后再次绑定。
由于数据来自linq,在绑定之前,你可以这样做:
remove the item from the datasource, and bind again.
since the data comes from linq, before binding, you can do:
如果我理解正确的话,您想要删除用户从 DGV 中选择的行。
使用 DGV 的 DataGridViewRowCollection 而不是 DataTable 的 DataRowCollection。 DataGridViewRow 具有 Selected 属性,该属性指示是否选择了行。
一旦确定要删除某行,就可以使用 DataGridViewRowCollection 的 Remove 方法从网格中删除该项目,例如 YerDataGridView.Rows.Remove(row)
注意此时,虽然该项目已从 DGV 中删除,但仍未从 Access DB 中删除。您需要在 DataSet/DataTable 上调用 TableAdapter Update 方法将删除提交到 DB,例如 YerTableAdapter.Update(YerDataSet)
我通常会在删除所有要从数据库中删除的项目后调用一次 Update 来提交更改DGV。
If I understand you correctly, you want to delete rows selected by a user from your DGV.
Use the DataGridViewRowCollection of your DGV rather than the DataRowCollection of the DataTable. The DataGridViewRow has the Selected property that indicates whether a row is selected or otherwise.
Once you have determined that a row is to be deleted, you can use the Remove method of the DataGridViewRowCollection to delete the item from the grid, e.g. YerDataGridView.Rows.Remove(row)
Note that at this point, although the item is removed from the DGV, it still has not been deleted from the Access DB. You need to call the TableAdapter Update method on your DataSet/DataTable to commit the deletions to the DB, e.g. YerTableAdapter.Update(YerDataSet)
I normally would call Update once to commit the changes only after having removed all the items to be deleted from the DGV.