我会尽力解释我的问题。
我有一堂课:
public class Person()
{
[Browsable(false)]
public Int32 Id { get; set; }
public string Name { get; set; }
//...
}我使用PropertyGrid控件显示Name字段,但不需要显示Id,因此我将Browsable属性设置为false,如下所示:
[Browsable(false)]
public Int32 Id { get; set; }在我的图形用户界面中,我在Person控件中显示了ListView类的所有元素,当选择一个元素时,我在PropertyGrid控件中显示属性如下:
void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
this.propertyGrid.SelectedObject = (object)this.listView.SelectedObject;
}一切正常,PropertyGrid只显示字段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.ArgumentException‘类型的未处理异常发生在System.Windows.Forms.dll中
附加信息:无法绑定到新的显示成员.
如何解决这个问题?
PS:如果我删除[Browsable(false)]行,一切都正常,但是PropertyGrid控件中的Id字段将显示出来。
发布于 2013-07-23 18:45:58
我重复了这个问题,并在设置了DataSource和ValueMember属性之后,通过设置DisplayMember和ValueMember属性来解决这个问题:
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Id";
comboBox1.DataSource = new BindingSource(people, null);https://stackoverflow.com/questions/17816708
复制相似问题