【问题标题】:Binding Combobox Using Dictionary as the Datasource使用字典作为数据源绑定组合框
【发布时间】:2021-08-08 17:05:09
【问题描述】:

我正在使用 .NET 2.0,并且正在尝试将组合框的数据源绑定到已排序的字典。

所以我得到的错误是“DataMember property 'Key' cannot be found on the Datasource”。

        SortedDictionary<string, int> userCache = UserCache.getSortedUserValueCache();
        userListComboBox.DataSource = new BindingSource(userCache, "Key"); //This line is causing the error
        userListComboBox.DisplayMember = "Key";
        userListComboBox.ValueMember = "Value";

【问题讨论】:

    标签: c# .net winforms combobox datasource


    【解决方案1】:
    SortedDictionary<string, int> userCache = new SortedDictionary<string, int>
    {
      {"a", 1},
      {"b", 2},
      {"c", 3}
    };
    comboBox1.DataSource = new BindingSource(userCache, null);
    comboBox1.DisplayMember = "Key";
    comboBox1.ValueMember = "Value";
    

    但是为什么要将ValueMember 设置为“Value”,它不应该绑定到“Key”(并且DisplayMember 也绑定到“Value”)?

    【讨论】:

    • 好吧,这两种方式都不重要。但将两者互换可能更有意义。但是我遇到的问题是“comboBox1.DataSource = new BindingSource(userCache, null);”我不能在那里拉 null 因为它给了我一个错误。
    • “ArgumentException:无法绑定到新的显示成员。参数名称:newDisplayMember。”我不知道 user803952 遇到​​了什么错误,但这是我尝试使用 IDictionary&lt;int, string&gt; 执行此操作时遇到的错误
    • 当我尝试将 Dictionary 绑定到组合框并收到异常“复杂数据绑定接受 IList 或 IListSource 作为数据源”时,此答案对我有用。跨度>
    • 您可能想尝试的另一件事是将行comboBox1.DataSource = new BindingSource(userCache, null);设置 DisplayMember 和 ValueMember 后向下
    • 有时,如果在 DisplayMember 之前分配了 DataSource,则在 DisplayMember 分配行处执行块。对我来说,这是可行的—— cBox.DataSource = null; cBox.DisplayMember = "值"; cBox.ValueMember = "键"; cBox.DataSource = new BindingSource(dict, null); // @dmihailescu 是对的
    【解决方案2】:

    我使用了 Sorin Comanescu 的解决方案,但在尝试获取所选值时遇到了问题。我的组合框是一个工具条组合框。我使用了“组合框”属性,它公开了一个普通的组合框。

    我有一个

     Dictionary<Control, string> controls = new Dictionary<Control, string>();
    

    绑定代码(Sorin Comanescu 的解决方案 - 效果很好):

     controls.Add(pictureBox1, "Image");
     controls.Add(dgvText, "Text");
     cbFocusedControl.ComboBox.DataSource = new BindingSource(controls, null);
     cbFocusedControl.ComboBox.ValueMember = "Key";
     cbFocusedControl.ComboBox.DisplayMember = "Value";
    

    问题是当我试图获取选定的值时,我没有意识到如何检索它。经过几次尝试,我得到了这个:

     var control = ((KeyValuePair<Control, string>) cbFocusedControl.ComboBox.SelectedItem).Key
    

    希望对其他人有所帮助!

    【讨论】:

    • 这行得通,我在自己的代码中使用了以下代码来让它工作。 gist.github.com/psykzz/5374823
    • 你也可以这样做(获取选中的值): var value = comboBox.SelectedItem; var someItem = value.GetType().GetProperty("Key").GetValue(value, null);
    • 最后一行也可以简化为: var control = ((KeyValuePair) cbFocusedControl.ComboBox.SelectedItem).Key; 1) 不必要的外括号,2) 编译器已经知道 Key 是 Control,因为它正在转换为 KeyValuePair,因此不需要转换为 Control。
    • @AdamMarshall 感谢您的建议。根据它编辑了答案。
    • 除此之外,您为什么不能只使用cbFocusedControl.ComboBox.SelectedTextcbFocusedControl.ComboBox.SelectedValue 分别获取DisplayMember 和ValueMember?
    【解决方案3】:
            var colors = new Dictionary < string, string > ();
            colors["10"] = "Red";
    

    绑定到组合框

            comboBox1.DataSource = new BindingSource(colors, null);
            comboBox1.DisplayMember = "Value";
            comboBox1.ValueMember = "Key"; 
    

    完整源码...Dictionary as a Combobox Datasource

    杰瑞

    【讨论】:

      【解决方案4】:
      userListComboBox.DataSource = userCache.ToList();
      userListComboBox.DisplayMember = "Key";
      

      【讨论】:

      • 干净简单。
      【解决方案5】:

      字典不能直接作为数据源,应该多做点。

      SortedDictionary<string, int> userCache =  UserCache.getSortedUserValueCache();
      KeyValuePair<string, int> [] ar= new KeyValuePair<string,int>[userCache.Count];
      userCache.CopyTo(ar, 0);
      comboBox1.DataSource = ar; new BindingSource(ar, "Key"); //This line is causing the error
      comboBox1.DisplayMember = "Value";
      comboBox1.ValueMember = "Key";
      

      【讨论】:

        【解决方案6】:

        我知道这是一个很老的话题,但我也遇到了同样的问题。

        我的解决方案:

        我们如何填充组合框:

        foreach (KeyValuePair<int, string> item in listRegion)
        {
            combo.Items.Add(item.Value);
            combo.ValueMember = item.Value.ToString();
            combo.DisplayMember = item.Key.ToString();
            combo.SelectedIndex = 0;
        }
        

        我们就是这样进入的:

         MessageBox.Show(combo_region.DisplayMember.ToString());
        

        希望对大家有所帮助

        【讨论】:

          【解决方案7】:

          如果这不起作用,为什么不简单地在字典上执行一个 foreach 循环,将所有项目添加到组合框?

          foreach(var item in userCache)
          {
              userListComboBox.Items.Add(new ListItem(item.Key, item.Value));
          }
          

          【讨论】:

          • 绑定和添加项目不是一回事。好吧,也许这就是 OP 真正需要的,谁知道呢? ;)
          • 我知道,但我没有看到任何依赖于数据绑定本身的代码。
          • 好吧,您的建议可行,但 System.Web.UI.WebControls 命名空间中存在“新 ListItem”,我不会为 Windows 窗体应用程序导入它。
          • 我也不会那样做,我认为有一个等效的 winforms 吗?我不喜欢winforms。
          • new ComboBoxItem("itemtext","itemid");可以改用
          【解决方案8】:

          使用 -->

          comboBox1.DataSource = colors.ToList();
          

          除非将字典转换为列表,否则组合框无法识别其成员。

          【讨论】:

            【解决方案9】:

            试着这样做......

            SortedDictionary<string, int> userCache = UserCache.getSortedUserValueCache();
            
                // Add this code
                if(userCache != null)
                {
                    userListComboBox.DataSource = new BindingSource(userCache, null); // Key => null
                    userListComboBox.DisplayMember = "Key";
                    userListComboBox.ValueMember = "Value";
                }
            

            【讨论】:

              猜你喜欢
              • 2016-09-17
              • 1970-01-01
              • 1970-01-01
              • 2020-07-18
              • 1970-01-01
              • 1970-01-01
              • 2011-06-28
              • 2013-09-03
              相关资源
              最近更新 更多