从Dictionary加载一个ComboBox,BindingSource给出一个ArgumentNullException

时间:2015-03-17 20:48:46

标签: c# windows-mobile compact-framework

我正在尝试从Dictionary中初始化ComboBox,如下所示:

Dictionary<string, int> TestDictionary = new Dictionary<string, int>
{
    {"Text1", 1},
    {"Text2", 2},
    {"Text3", 3}
};

testComboBox.DataSource = new BindingSource(TestDictionary, null);

但这引发了以下异常:

ArgumentNullException

1 个答案:

答案 0 :(得分:0)

为什么要将它绑定到null? https://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource%28v=vs.110%29.aspx 你想要这个人

BindingSource(IContainer)

不是这个

BindingSource(Object, String)

由于

public BindingSource(
    Object dataSource,
    string dataMember
)

dataMember由组合框使用。

dataMember
Type: System.String
The specific column or list name within the data source to bind to.

或者给它一个正确的名字。

UPD /校正:

根据评论顶部,您需要映射这两个:

testComboBox.DisplayMember = "Value";
testComboBox.ValueMember = "Key";