Reputation: 1457
I will try to explain my issue.
I have got a class:
public class Person()
{
[Browsable(false)]
public Int32 Id { get; set; }
public string Name { get; set; }
//...
}
I use PropertyGrid
control to show Name
field, but I don't need to show Id
, so I set Browsable
property to false like this:
[Browsable(false)]
public Int32 Id { get; set; }
In my GUI I present all elements of Person
class in ListView
control, and when an element is selected I show properties in PropertyGrid
control like this:
void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
this.propertyGrid.SelectedObject = (object)this.listView.SelectedObject;
}
Everything works fine, PropertyGrid
shows only field Name
.
Then I need to use ComboBox
control like this:
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";
And on line this.comboBox.ValueMember = "Id";
got this error:
An unhandled exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll
Additional information: Cannot bind to the new display member.
How to resolve this problem ?
PS: If I remove [Browsable(false)]
line everything works fine, but Id
field in PropertyGrid
control will be shown
Upvotes: 1
Views: 6082
Reputation: 81675
I duplicated the problem, and I solved it by setting the DataSource after setting the DisplayMember and ValueMember properties:
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Id";
comboBox1.DataSource = new BindingSource(people, null);
Upvotes: 5