【问题标题】:Combobox options not changing when datasource updated更新数据源时组合框选项不会更改
【发布时间】:2016-12-28 19:44:22
【问题描述】:

所以当我在主窗口构造函数中使用这个方法时数据被正确加载:

public void Load_Dropdown(configuration con)
{
    bindinglist = new BindingList<ListCollection.ListsList>(Get.ListCollection(con).List);
    BindingSource bSource = new BindingSource {DataSource = bindinglist};
    sharepointListSelect.DataSource = bSource;
    sharepointListSelect.DisplayMember = "Title";
    sharepointListSelect.ValueMember = "ID";
}

这很好用,但是当我稍后尝试调用类似的方法来更新选项时,什么也没有发生 - 因为下拉菜单中的选项不会改变。

我调用来更新它的方法如下所示:

public bool Reload_Dropdown(configuration con)
{            
    bindinglist = new BindingList<ListCollection.ListsList>(Get.ListCollection(con).List);
    BindingSource bSource = new BindingSource { DataSource = bindinglist };
    sharepointListSelect.DataSource = bSource; 
    sharepointListSelect.DisplayMember = "Title";
    sharepointListSelect.ValueMember = "ID";


    //There is other logic here, but i left it out as it isn't relevant
    return true;
}

顺便说一句,绑定列表对象在类中初始化为:

private BindingList<ListCollection.ListsList> bindinglist;

我知道,现在我可以重用第一种方法 - 但我拆分声明以调试为什么它首先会导致更新失败。

【问题讨论】:

  • 考虑发布minimal reproducible example。第一步创建 MCVE 将帮助您找到并解决问题。如果您自己无法解决问题,那么它将帮助其他用户重现问题以帮助您。另外,不要一遍又一遍地创建 BindingSource 的不同实例,只需在设计时在表单上放置一个 BindingSource 并使用它。
  • 我发现了问题所在。这是我的疏忽,我会做出回答来描述我是如何处理这个问题的。

标签: c# winforms data-binding combobox


【解决方案1】:

我很愚蠢-所以问题不在于任何一种方法,而是我如何调用该方法。如果它不是静态的,则不能从另一个表单调用方法。对我来说有很大的疏忽——我想我有点太习惯于 MVC 了。

只要您从同一方法中调用它,此解决方案就可以正常工作。我选择只使用一个按钮来刷新下拉列表。

【讨论】:

    猜你喜欢
    • 2011-03-30
    • 2019-08-08
    • 2011-02-13
    • 1970-01-01
    • 1970-01-01
    • 2011-08-17
    • 2022-01-19
    • 2018-04-03
    • 1970-01-01
    相关资源
    最近更新 更多