C# - 如何在删除行后刷新 DataGridView [英] C# - how do I refresh DataGridView after removing rows

查看:98
本文介绍了C# - 如何在删除行后刷新 DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,我需要在重复间隔后从 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();

}

我知道这些行已从 DataGridView 中成功删除,但无论出于何种原因它们仍保留在显示中.关于我可能做错的任何提示?

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?

推荐答案

如果你已经将你的数据网格绑定到一个 Observable 集合(如果没有,那么你应该)那么你将需要实现 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

这篇关于C# - 如何在删除行后刷新 DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆