Cannot bind to the new display member in ComboBox

后端 未结 9 683
迷失自我
迷失自我 2021-01-01 21:14

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         


        
相关标签:
9条回答
  • 2021-01-01 21:22

    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;
    
    0 讨论(0)
  • 2021-01-01 21:30
    if (lstItems.Count > 0)
    {
        comboBox1.DataSource = lstItems;
        comboBox1.DisplayMember = "Name";
        comboBox1.ValueMember = "Id";
    }
    
    0 讨论(0)
  • 2021-01-01 21:32

    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; }
    
    0 讨论(0)
  • 2021-01-01 21:32

    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.

    0 讨论(0)
  • 2021-01-01 21:33

    In my case I was binding to a DataSet when I thought I was binding to a DataTable.

    0 讨论(0)
  • 2021-01-01 21:42

    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.

    0 讨论(0)
提交回复
热议问题