1

我将尝试解释我的问题。

我有一堂课:

public class Person()
{
        [Browsable(false)]
        public Int32 Id { get; set; }

        public string Name { get; set; }

        //...
}

我使用PropertyGridcontrol 来显示Name字段,但我不需要 show Id,所以我将Browsable属性设置为 false ,如下所示:

[Browsable(false)]
public Int32 Id { get; set; }

In my GUI I present all elements of Personclass in ListViewcontrol, and when an element is selected I show properties in PropertyGridcontrol like this:

void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
   this.propertyGrid.SelectedObject = (object)this.listView.SelectedObject;
}

一切正常,PropertyGrid只显示 field Name

然后我需要ComboBox像这样使用控制:

List<Person> people = new List<Person>();
people.Add(...)
//.....

this.comboBox.DataSource = new BindingSource(people, null);
this.comboBox.ValueMember = "Id"; // here an exeption has been thrown !!!
this.comboBox.DisplayMember = "Name";

并在线this.comboBox.ValueMember = "Id";得到这个错误:

System.Windows.Forms.dll 中出现“System.ArgumentException”类型的未处理异常

附加信息:无法绑定到新的显示成员。

如何解决这个问题?

PS:如果我删除[Browsable(false)]行一切正常,但将显示控制Id字段PropertyGrid

4

1 回答 1

5

我重复了这个问题,并通过在设置 DisplayMember 和 ValueMember 属性后设置 DataSource 来解决它:

comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Id";
comboBox1.DataSource = new BindingSource(people, null);
于 2013-07-23T18:45:58.873 回答