问题描述
我有一个作为用户控件创建的组合框(它实际上由标签、组合框和文本框组成).我正在尝试将数据集绑定到组合框数据源,但我不断收到有关 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!
谢谢
推荐答案
不再使用 Datatset.创建了一个将保存 ID 和名称值的类(设施").将信息"更改为列表类型.然后下面的代码工作得很好:
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";
绑定没有问题.
这篇关于ComboBox 问题:无法绑定到新值成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!