ComboBox Issue: Cannot bind to new value member

后端 未结 6 1632
甜味超标
甜味超标 2021-01-14 08:02

I\'v got a combobox that I created as a user control(it\'s actually made up of a label, combobox and textbox). I\'m trying to bind a dataset to the combobox datasource, but

相关标签:
6条回答
  • 2021-01-14 08:03

    I had the same issue and found a solution that may not apply to your problem, but might be helpful for others:

    A property marked with the [Browsable(false)]-Attribute will also cause this exception when trying to bind to it.

    0 讨论(0)
  • 2021-01-14 08:16

    Moved away from using a Datatset. Created a class("Facilities") that will hold an ID and Name values. Changed "info" to a List type. Then the following code worked just fine:

    info.Add(new Facilities { ID = dr["other_facility_id"].ToString(), Name = dr["other_facility_name"].ToString() });
    
      ((ucComboBox)ctrl).combobox.DataSource = new BindingSource(info, null);//info;
      ((ucComboBox)ctrl).combobox.ValueMember = "ID";
      ((ucComboBox)ctrl).combobox.DisplayMember = "Name";
    

    No issues with binding that.

    0 讨论(0)
  • 2021-01-14 08:18

    I had same issue while binding. I reversed the order and everything started working. The original code will look like this

      ((ucComboBox)ctrl).combobox.ValueMember = "radiology_id";
      ((ucComboBox)ctrl).combobox.DisplayMember = "radiology_name";
      ((ucComboBox)ctrl).combobox.DataSource = info;
    
    0 讨论(0)
  • 2021-01-14 08:19

    Surely the fragment of code below is going to cause an issue?

    public string value
    {
        get { return _value ; }
        set { _value = value; }
    }
    

    You need to name this something else e.g. comboValue. "value" represents the implicit variable passed into a property declaration.

    i.e.

    public string comboValue
    {
        get { return _value ; }
        set { _value = value; }
    }
    
    0 讨论(0)
  • 2021-01-14 08:22

    I think you have the property Modifiers of your combo to something different from Public.
    However I will choose to implement two new public properties at the usercontrol level.
    DisplayMember and ValueMember just to avoid that ugly cast.
    In the set/get accessor I will reflect the values in/from the internal combo

    public string DisplayMember
    { 
        get { return combobox1.DisplayMember; } 
        set { combobox1.DisplayMember = value;} 
    } 
    public string ValueMember 
    { 
        get { return combobox1.ValueMember; } 
        set { combobox1.ValueMember = value;} 
    } 
    
    0 讨论(0)
  • 2021-01-14 08:27

    Also make sure the ID and Name properties are Public otherwise you get the error:
    Cannot bind to the new display member.

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