我遇到了同样的问题,但我正在使用泛型。我已经使用组合框的绑定上下文来摆脱它。(当您不知道绑定列表的大小时非常有用 - 在您的情况下它是 5 个项目)
在下面的代码中,DisplayBindItem 只是一个具有 Key 和 Value 的类。
List<DisplayBindItem> cust = (from x in _db.m01_customers
where x.m01_customer_type == CustomerType.Member.GetHashCode()
select new DisplayBindItem
{
Key = x.m01_id.ToString(),
Value = x.m01_customer_name
}).ToList();
cmbApprover1.BindingContext = new BindingContext();
cmbApprover1.DataSource = cust;
cmbApprover1.DisplayMember = "Value";
cmbApprover1.ValueMember = "Key";
//This does the trick :)
cmbApprover2.BindingContext = new BindingContext();
cmbApprover2.DataSource = cust ;
cmbApprover2.DisplayMember = "Value";
cmbApprover2.ValueMember = "Key";
该类供您参考。
public class DisplayBindItem
{
private string key = string.Empty;
public string Key
{
get { return key; }
set { key = value; }
}
private string value = string.Empty;
public string Value
{
get { return this.value; }
set { this.value = value; }
}
public DisplayBindItem(string k, string val)
{
this.key = k;
this.value = val;
}
public DisplayBindItem()
{ }
}
如果这解决了您的问题,请标记为答案。