C# - 删除行后如何刷新 DataGridView
在我的代码中,我需要在重复间隔后从 DataGridView 中删除行,因此当计时器到期时我调用以下函数:
private void removeRows(DataGridView dgv) {
foreach (DataGridViewRow row in dgv.Rows)
{
// if some condition holds
dgv.Remove(row);
}
dgv.Refresh();
}
我知道这些行已成功从 DataGridView 中删除,但无论出于何种原因它们仍然保留在显示中。关于我可能做错了什么有什么建议吗?
In my code I need to remove rows from the DataGridView after a recurring interval, and so I call the following function when a timer expires:
private void removeRows(DataGridView dgv) {
foreach (DataGridViewRow row in dgv.Rows)
{
// if some condition holds
dgv.Remove(row);
}
dgv.Refresh();
}
I know the rows are successfully deleted from the DataGridView, though they still remains in the display for whatever reason. Any tips on what I might be doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
不需要重新绑定数据网格吗?
?
Don't you need to rebind the data grid?
?
有时刷新数据 gridview 是不够的,它的包含父级也应该刷新。
试试这个:
您还可以编辑源并将新数据源附加到控件。
Sometimes refreshing the data gridview is not enough and its containing parent should be refreshed too.
Try this:
You could also edit your source and attach the new datasource to the control.
这段代码可能有用:
希望这有帮助。
this code could be useful:
Hope this helps.
如果您已将数据网格绑定到可观察集合(如果没有,那么您应该这样做),那么您将需要实现 INotifyCollectionChanged 接口,以便侦听器收到动态更改的通知,例如添加和删除项目或刷新整个列表时。
华泰
If you have bound your datagrid to an Observable Collection (if not then you should) then you will need to implement INotifyCollectionChanged interface so that listeners are notified of dynamic changes, such as when items get added and removed or the whole list is refreshed.
HTH
如果我理解正确的话,您想要删除用户从 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.
如果它是数据绑定网格,您应该处理绑定源本身而不是网格。
If it's a data-bound grid, you should be working on the binding source itself instead of the grid.
尝试从绑定源中删除实际项目。
Try removing the actual items from your binding source instead.
我遇到了同样的问题,我已经找到了根本原因。
您所要做的就是在重新加载之前初始化数据库上下文对象并查看魔法。
I had same problem and I have find out the root cause.
all you have to do initialize you database context object in before reload and see the magic .