【问题标题】:Load a ComboBox from Dictionary with BindingSource gives an ArgumentNullException使用 BindingSource 从 Dictionary 加载 ComboBox 会产生 ArgumentNullException
【发布时间】:2015-05-20 11:43:30
【问题描述】:

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

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

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

但这会引发以下异常:

【问题讨论】:

标签: c# windows-mobile compact-framework


【解决方案1】:

为什么要将它绑定到 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";

【讨论】:

  • 我试过了,但是 Collections.Generic.Dictionary 没有实现 ComponentModel.IContainer 接口。
猜你喜欢
  • 2019-08-18
  • 1970-01-01
  • 2012-01-18
  • 1970-01-01
  • 2022-12-17
  • 1970-01-01
  • 1970-01-01
  • 2011-05-27
  • 1970-01-01
相关资源
最近更新 更多