C# ComboBox 问题:无法绑定到新值成员
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9421546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ComboBox Issue: Cannot bind to new value member
提问by Encryption
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:
我有一个作为用户控件创建的组合框(它实际上由标签、组合框和文本框组成)。我正在尝试将数据集绑定到组合框数据源,但我不断收到有关 ValueMember/Display 成员的错误消息:
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.
我在用户控件中有一些其他属性和事件,但它们不应该是问题。
My code to bind the info:
我绑定信息的代码:
((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!
我尝试了很多不同的策略,但都没有成功。请帮忙!
Thanks
谢谢
采纳答案by Encryption
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:
不再使用数据集。创建了一个将保存 ID 和名称值的类(“设施”)。将“信息”更改为列表类型。然后下面的代码工作得很好:
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.
绑定没有问题。
回答by kaj
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.
您需要将其命名为其他名称,例如 comboValue。“value”表示传递到属性声明中的隐式变量。
i.e.
IE
public string comboValue
{
get { return _value ; }
set { _value = value; }
}
回答by Steve
I think you have the property Modifiersof your combo to something different from Public.
However I will choose to implement two new public properties at the usercontrol level.DisplayMemberand ValueMemberjust to avoid that ugly cast.
In the set/get accessor I will reflect the values in/from the internal combo
我认为您的组合的属性Modifiers与Public不同。
但是,我将选择在用户控件级别实现两个新的公共属性。DisplayMember而ValueMember只是为了避免难看的演员。
在 set/get 访问器中,我将反映内部组合中的值/来自内部组合的值
public string DisplayMember
{
get { return combobox1.DisplayMember; }
set { combobox1.DisplayMember = value;}
}
public string ValueMember
{
get { return combobox1.ValueMember; }
set { combobox1.ValueMember = value;}
}
回答by Jeremy Thompson
Also make sure the ID and Name properties are Publicotherwise you get the error:Cannot bind to the new display member.
还要确保 ID 和 Name 属性是公共的,否则会出现错误:Cannot bind to the new display member.
回答by hemant gautam
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;
回答by Kevin Wienhold
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.
用[Browsable(false)]-Attribute标记的属性在尝试绑定时也会导致此异常。

