Cannot bind to the new display member in ComboBox
The accepted answer is not correct.
The Op needed to change the order of how he was assigning DisplayMember
, ValueMember
and Datasource
and then note the added line of code.
comboBox1.DisplayMember="Name";
comboBox1.ValueMember = "Id";
comboBox1.DataSource = lstItems;
comboBox1.BindingContext = this.BindingContext;
In my case, I did bind to properties that had the Browsable
attribute set to false
.
I.e. the error occurred because of:
[Browsable(false)]
public string MyProperty { get; set; }
And I did resolve it by simply removing the attribute:
public string MyProperty { get; set; }
You should make Name
and Id
properties. You can't bind ComboBox
to fields.
public string Name { get; set; }
public int Id { get; set; }
It's also stated in docs:
ValueMember Property
: Gets or sets the property to use as the actual value for the items in the System.Windows.Forms.ListControl.