绑定到BindingList的DataGridView在值更改时不刷新 [英] DataGridView bound to BindingList does not refresh when value changed

查看:1023
本文介绍了绑定到BindingList的DataGridView在值更改时不刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataGridView绑定到一个BindingList(C#Windows窗体)。如果我更改列表中某个项目中的一个值,它不会立即显示在网格中。如果我点击更改的单元格,或最小化,然后最大化窗口,它正确更新,但我需要它自动发生。



我以前有同样的问题,但在那个情况我不得不改变单元格的背景颜色在同一时间值改变。这导致单元格正确刷新。



我可以让它工作的唯一方法是...

  dataGridView.DataSource = null; 
dataGridView.DataSource = myBindingList

...但我真的很想避免这个因为它使滚动条弹回到顶部,意味着我必须再次设置我的单元格背景颜色。当然有更好的方法。我已经尝试刷新(以及刷新父级),更新和无效,但他们没有做我需要的。



我看到这个问题在几个留言板上提到,但还没有看到一个工作的答案。

解决方案


ListChanged 仅当列表项类型实现 INotifyPropertyChanged 接口时,才会引发项目值更改通知。


取自: http://msdn.microsoft.com/en-us/library/ms132742.aspx



所以我的第一个问题是:实现你的输入 INotifyPropertyChanged 正确?


I have a DataGridView bound to a BindingList (C# Windows Forms). If I change one of the values in an item in the list it does not immediately show up in the grid. If I click on the changed cell, or minimize then maximize the window it updates properly, but I need it to happen automatically.

I had the same problem earlier, but in that situation I had to change the cell's background colour at the same time that the value changed. This caused the cell to refresh correctly.

The only way I can get it to work is...

dataGridView.DataSource = null;  
dataGridView.DataSource = myBindingList

...but I'd really like to avoid this as it makes the scrollbar pop back to the top, and means that I'd have to set my cell background colours again. Surely there's a better way. I've tried Refresh (as well as refreshing the parent), Update, and Invalidate, but they're not doing what I need.

I've seen this problem mentioned on a few message boards, but haven't seen a working answer to it yet.

解决方案

ListChanged notifications for item value changes are only raised if the list item type implements the INotifyPropertyChanged interface.

Taken from: http://msdn.microsoft.com/en-us/library/ms132742.aspx

So my first question would be: Implements your item the INotifyPropertyChanged properly?

这篇关于绑定到BindingList的DataGridView在值更改时不刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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