C# BindingSource到ComboBox和[Browsable(false)]属性到PropertyGrid don';我们不能一起工作
我会尽力解释我的问题 我有一门课:C# BindingSource到ComboBox和[Browsable(false)]属性到PropertyGrid don';我们不能一起工作,c#,.net,winforms,combobox,propertygrid,C#,.net,Winforms,Combobox,Propertygrid,我会尽力解释我的问题 我有一门课: public class Person() { [Browsable(false)] public Int32 Id { get; set; } public string Name { get; set; } //... } 我使用PropertyGrid控件来显示Name字段,但我不需要显示Id,所以我将Browsable属性设置为false,如下所示: [Browsable(false
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; }
在我的GUI中,我在ListView
控件中显示Person
类的所有元素,当选择一个元素时,我在PropertyGrid
控件中显示如下属性:
void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
this.propertyGrid.SelectedObject = (object)this.listView.SelectedObject;
}
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";
一切正常,PropertyGrid
只显示字段Name
然后我需要像这样使用ComboBox
控件:
void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
this.propertyGrid.SelectedObject = (object)this.listView.SelectedObject;
}
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";
List people=newlist();
人物。添加(…)
//.....
this.comboBox.DataSource=newbindingsource(people,null);
this.comboBox.ValueMember=“Id”//这里抛出了一个例外!!!
this.comboBox.DisplayMember=“Name”;
并在线this.comboBox.ValueMember=“Id”代码>获取此错误:
System.Windows.Forms.dll中发生“System.ArgumentException”类型的未处理异常
其他信息:无法绑定到新的显示成员。
如何解决这个问题
PS:如果我删除[Browsable(false)]
行,一切正常,但是PropertyGrid
控件中的Id
字段将被显示我复制了这个问题,我在设置DisplayMember和ValueMember属性后通过设置数据源解决了这个问题:
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Id";
comboBox1.DataSource = new BindingSource(people, null);
我复制了这个问题,并通过在设置DisplayMember和ValueMember属性之后设置数据源来解决它:
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Id";
comboBox1.DataSource = new BindingSource(people, null);
这似乎是一个已知的问题:…和…这似乎是一个已知的问题:…和。。。