c# - 使用字典作为数据源绑定(bind)组合框

标签 c# .net winforms combobox datasource

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

所以我收到的错误是“无法在数据源上找到 DataMember 属性‘Key’”。

        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”,它不应该绑定(bind)到“Key”(并且 DisplayMember 也绑定(bind)到“Value”)?

关于c# - 使用字典作为数据源绑定(bind)组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6412739/

相关文章:

c# - Windows自定义控件如何实现数据绑定(bind)?

c# - 在 LINQ 中采用所有三元运算符?

c# - WinForms 浏览器控件和 IE 的消亡

c# - Visual Studio IDE0059 C# 不必要的值赋值错误?

.net - 如何判断类型 A 是否可隐式转换为类型 B

c# - 如何为列表框中的每个项目设置不同的工具提示文本?

c# - MVC 5 - jQueryUI 对话框 View 不显示

c# - 程序 '[12604] iisexpress.exe' 已退出,代码为 0 (0x0)

.net - 为什么我们仍然在.NET中使用数据集?

c# 内存游戏,需要在游戏结束时添加消息