使用字典作为数据源绑定组合框 [英] Binding Combobox Using Dictionary as the Datasource

查看:18
本文介绍了使用字典作为数据源绑定组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using .NET 2.0 and I'm trying to bind a combobox's Datasource to a sorted dictionary.

所以我得到的错误是在数据源上找不到 DataMember 属性 'Key'".

So the error I'm getting is "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";

推荐答案

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")?

But why are you setting the ValueMember to "Value", shouldn't it be bound to "Key" (and DisplayMember to "Value" as well)?

这篇关于使用字典作为数据源绑定组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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