当数据源更改时,ComboBox 不会自动更新?

发布于 2024-12-03 18:50:23 字数 435 浏览 2 评论 0原文

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

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

为了在我编辑项目时让它更新,而不是要创建或删除项目,我必须在更改后执行此操作:

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

这解决了问题,但似乎是一个相当混乱的解决方案。此外,对于大型列表,它可能会变得很慢(我知道这是过早的优化),但仍然有办法强制 ComboBox 更新而不完全重新分配其数据源?

感谢您的阅读。

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?

Thanks for reading.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雨落星ぅ辰 2024-12-10 18:50:23

MSDN 论坛中是这么说的:

IBindingList 接口包含 ListChanged 事件,其中
像组合框这样的控件连接到底层数据源
分配给它的实现了所述接口。你的数据源必须
使用适当的 ListChangeEventArgs 引发相应的 ListChanged 如果
无论您添加、删除、更改等您的 IBindingList 实现者。
这样,每当您用来绑定到您的底层源时
组合框改变,对应的UI控件(组合框)为
焕然一新。

您说您正在使用 BindingList,实际上您确实获得了组合框来反映添加或删除项目事件。我认为您应该以另一种方式更新 BindingList 中已有的项目,因为看起来正确的 BindingList 事件没有触发。

您可以对此进行调查,或者干脆接受重置并重新分配数据源,我认为这还不错,您处于 Statefull Windows Forms 应用程序中,而不是 SatetLess Webforms 中,因此您的对象始终存在:)

this is stated in the MSDN forums:

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.

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.

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 :)

呆橘 2024-12-10 18:50:23

您需要可观察集合和 IPropertyChange 实现:

绑定到 ObservableCollection 的 ComboBox 不会更新

You need observable collections and IPropertyChange implementation:

ComboBox bound to a ObservableCollection does not update

毁虫ゝ 2024-12-10 18:50:23

我遇到了类似的问题,组合框不会填充它的选项列表,因为对象没有被渲染。第一个示例运行时导致“索引无效”错误。我通过在设置索引之前显示控件来解决这个问题。

ComboBox cb = new ComboBox() {
            DataSource = DataSource,
    };
        cb.SelectedIndex = index;
        this.Controls.Add(cb);
        cb.Update();
        
     

进入:

ComboBox cb = new ComboBox();
        this.Controls.Add(cb);
        cb.Update();
        cd.DataSource = DataSource
        cb.SelectedIndex = index;

I had a similar issue where the combobox wouldn't populate it's list of options because the object wasn't being rendered. Leading to a "Index invalid" error when the first example ran. I worked around this by displaying the control before setting the index.

ComboBox cb = new ComboBox() {
            DataSource = DataSource,
    };
        cb.SelectedIndex = index;
        this.Controls.Add(cb);
        cb.Update();
        
     

Into:

ComboBox cb = new ComboBox();
        this.Controls.Add(cb);
        cb.Update();
        cd.DataSource = DataSource
        cb.SelectedIndex = index;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文