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

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

问题描述

我使用.NET 2.0和我想要一个组合框的数据源绑定到一个排序的字典。

所以我得到的错误是成员属性钥匙无法在数据源中找到。

  SortedDictionary<字符串,INT> userCache = UserCache.getSortedUserValueCache();
        userListComboBox.DataSource =新的BindingSource(userCache,钥匙); //此行导致错误
        userListComboBox.DisplayMember =关键;
        userListComboBox.ValueMember =值;
 

解决方案

  SortedDictionary<字符串,INT> userCache =新SortedDictionary<字符串,INT>
{
  {一个,1},
  {的b,2},
  {C,3}
};
comboBox1.DataSource =新的BindingSource(userCache,NULL);
comboBox1.DisplayMember =关键;
comboBox1.ValueMember =值;
 

但是,你为什么要设置 ValueMember 到值,应该不是被绑定到关键(与的DisplayMember 以价值为好)?

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

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";

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天全站免登陆