无法绑定到 ComboBox 中的新显示成员 [英] Cannot bind to the new display member in ComboBox
本文介绍了无法绑定到 ComboBox 中的新显示成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类给我这个错误
I have a class which give me this error
public class Item
{
public string Name;
public int Id
public Item(string name, int id)
{
Name = name;
Id = id;
}
}
这是我的功能
var lstItems = new List<Item>();
while(...)
{
lstItems.Add(new Item(sAd_Ref, Convert.ToInt32(sAd_ID)));
}
comboBox1.DataSource = lstItems;
comboBox1.ValueMember = "Id";
comboBox1.DisplayMember = "Name";
在倒数第二行,我得到了
On second to the last line I am getting exception of
无法绑定到新的显示成员.
Cannot bind to the new display member.
推荐答案
你应该制作 Name
和 Id
属性.您不能将 ComboBox
绑定到字段.
You should make Name
and Id
properties. You can't bind ComboBox
to fields.
public string Name { get; set; }
public int Id { get; set; }
文档中也有说明:
ValueMember 属性
:获取或设置属性以用作 System.Windows.Forms.ListControl 中项目的实际值.
ValueMember Property
: Gets or sets the property to use as the actual value for the items in the System.Windows.Forms.ListControl.
这篇关于无法绑定到 ComboBox 中的新显示成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文