在数据源上找不到数据成员属性“系统" [英] datamember property 'System' cannot be found on the DataSource
问题描述
我将一个 Dictionary
绑定到一个 ComboBox
.这似乎工作正常,没有任何编译错误,但在运行时我收到错误:
I am binding a Dictionary
to a ComboBox
. This seems to work alright without any compile errors, but at runtime I get the error:
在数据源上找不到数据成员属性系统"
datamember property 'System' cannot be found on the DataSource
这是我的代码:
public Dictionary<string, object> valuList
{
set
{
lComboBox.DataSource = new BindingSource(value,null);
lComboBox.DisplayMember = (value.Keys).ToString();
lComboBox.ValueMember = (value.Values).ToString();
}
}
Dictionary<string, string> x6 = new Dictionary<string, string>();
x6.Add("AS", "ASS");
x6.Add("AAS", "AASS");
myForm.valuList = x6;
推荐答案
如果你希望显示成员是字典中每一对的键,而值成员是每对的价值,你可能想要:
If you want the display member to be the key of each pair in the dictionary, and the value member to be the value of each pair, you probably want:
lComboBox.DisplayMember = "Key";
lComboBox.ValueMember = "Value";
您应该知道订单是不确定的 - 这真的是您想要的吗?
You should be aware that the order will be undetermined though - is that really what you want?
在Dictionary<,>.Keys
或Dictionary<,>.Values
的返回值上调用ToString()
可能是只是给你一个类型的完全限定名称,它将以 System.Collections...
开头 - 这就是你得到当前错误的原因.
Calling ToString()
on the return value of Dictionary<,>.Keys
or Dictionary<,>.Values
is probably just going to give you the fully-qualified name of a type, which will start with System.Collections...
- that's why you're getting the current error.
我还强烈建议您将您的属性重命名为遵循 .NET 命名约定的名称,并且意味着.
I'd also strongly encourage you to rename your property to something which follows the .NET naming conventions, and also means something.
这篇关于在数据源上找不到数据成员属性“系统"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!