I have a class which give me this error
public class Item
{
public string Name;
public int Id
public Item(string name, int id)
{
N
You can either check datasource null or bind datasource after setting value member and display member
cmbDepartment.DisplayMember = "Dep_Name";
cmbDepartment.ValueMember = "Dep_ID";
cmbDepartment.DataSource = model;
if (lstItems.Count > 0)
{
comboBox1.DataSource = lstItems;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Id";
}
In my case, I did bind to properties that had the Browsable
attribute set to false
.
I.e. the error occured because of:
[Browsable(false)]
public string MyProperty { get; set; }
And I did resolve it by simply removing the attribute:
public string MyProperty { get; set; }
tldr: Make sure the source you're binding to isn't empty.
I was getting the same error message
'Cannot bind to the new display member.Parameter name: newDisplayMember'
From code that looked like
bindingSource.DataSource = _MyDict;
comboBox.DisplayMember = "Value";
comboBox.ValueMember = "Key";
With the binding source already hooked up in the designer.
When you set the ValueMember
property the combobox will try to find an instance in your list to use reflection on (possibly to check the property actually exists). This fails when the source collection is currently empty.
In my case I was binding to a DataSet when I thought I was binding to a DataTable.
In my case I have solved this same problem by establishing the order in which I set the properties:
With ComboBoxPartNumber
.DisplayMember = "SuperSKU"
.ValueMember = "TareRequired"
.DataSource = _TblNP
.Enabled = True
.Refresh()
With
Here the _TblNP
field is a global local field of type System.Data.DataTable
.