组合框问题:无法绑定到新值成员组合、绑定、到新、成员

2023-09-07 15:21:04 作者:太监不懂皇帝的累 ≠

我有一个作为用户控件创建的组合框(它实际上由标签、组合框和文本框组成).我正在尝试将数据集绑定到组合框数据源,但在 ValueMember/Display 成员上不断收到错误消息:

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 I keep getting an error message on ValueMember/Display member:

Cannot bind to the new display member - newdisplay member
Cannot bind to the new value member - parameter name: value

我以为我已经为用户控件正确编码了所有内容:

I thought I had everything coded correctly for the usercontrol:

   public partial class ucComboBox : UserControl
{
    #region Properties (6) 
    private bool isEditableReadOnly;
    private bool ArrVisible;
    private string _value;
    private string _name;

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

我在用户控件中有一些其他属性和事件,但它们不应该是问题.

I have a few other properties and events in the usercontrol but they shouldnt be the issue.

我绑定信息的代码:

  ((ucComboBox)ctrl).combobox.DataSource = info;
  ((ucComboBox)ctrl).combobox.ValueMember = "radiology_id";
  ((ucComboBox)ctrl).combobox.DisplayMember = "radiology_name";

每当它击中值成员时它就会爆炸,并且我得到上述两个错误.我的用户控件中是否缺少某些内容?在我看来这应该可以工作..(仅供参考 - 信息数据源确实包含两列)

It blows up whenever it hits value member, and i get the two errors stated above. Am I missing something in my user control? Seems to me this should be working.. (fyi - the info datasource does contain the two columns)

我尝试了很多不同的策略,但都没有成功.请帮忙!

I've tried a bunch of different tactics with no success. Please help!

谢谢

推荐答案

不再使用数据集.创建了一个包含 ID 和 Name 值的类(Facilities").将信息"更改为列表类型.然后下面的代码就可以正常工作了:

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";

绑定没有问题.