我正在使用 .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/