组合框不会自动更新数据源发生变化时? [英] ComboBox doesn't automatically update when DataSource changes?

查看:363
本文介绍了组合框不会自动更新数据源发生变化时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

添加或删除相应从数据源的项目(一个简单的BindingList)组合框更新,但如果我编辑这样的一个项目,它不会自动更新时由于某些原因:

For some reason when adding or remove items from the DataSource (a simple BindingList) the ComboBox updates accordingly but if I edit an item like this, it doesn't update automatically:

myBindingList[index].Name = "NewName";
myBindingList[index].Value = newValue;

为了得到而不是创建或删除一个项目我来更新我编辑项目要做到这一点,改变之后:

In order to get it to update when I edit an item as opposed to creating or removing an item I have to do this after the change is made:

myComboBox.DataSource = null;
myComboBox.DataSource = myBindingList;

这解决了这个问题,但它似乎是一个相当混乱的解决方案。另外随着大型列表可能会变慢(过早的优化,我知道),但仍是有没有办法强制组合框没有完全重新分配其数据源更新?

This fixes the problem but it seems like a rather messy solution. Also with large lists it may become slow (premature optimization I know) but still is there a way to force the ComboBox to update without completely re-assigning its DataSource?

谢谢。阅读

推荐答案

这是在MSDN论坛上指出:

this is stated in the MSDN forums:

IBindingList的接口包含ListChanged事件,其中像组合框
控制挂钩到,如果基础数据源
分配给它实现了上述接口。您的数据源必须
提高相应的ListChanged适当ListChangeEventArgs如果
以往任何时候都添加,删除,变更等你IBindingList的实现者。
这样一来,只要您使用绑定到你的
组合框的底层源改变时,相应的UI控件(组合框)是
刷新。

The IBindingList interface contains the ListChanged event where controls like the combobox hook up into if the underlying datasource assigned to it implements the said interface. your datasource must raise the corresponding ListChanged with proper ListChangeEventArgs if ever you add, remove, change, etc. your IBindingList implementor. this way, whenever the underlying source you used to bind to your combobox is changed, the corresponding UI control (combobox) is refreshed.

你说你正在使用的BindingList,实际上你得到的组合框,以反映添加或删除项目的事件。我认为你应该做的项目的更新已在您的BindingList内以另一种方式,因为貌似正确的BindingList事件不会开火。

you say you are using BindingList and in fact you do get the combobox to reflect add or remove items events. I think you should do the update of the items already inside your BindingList in another way because looks like the proper BindingList events are not firing.

您既可以调查那或简单地复位生活和重新分配数据源,我不认为这是太糟糕了,你是有状态的Windows不SatetLess Web表单窗体应用程序让你有你的对象那里所有的时间:)

you could either investigate into that or simply live with reset and reassign the DataSource, I don't think is too bad, you are in Statefull Windows Forms application not in SatetLess Webforms so you do have your objects there all the time :)

这篇关于组合框不会自动更新数据源发生变化时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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