使用自定义业务对象列表作为 ComboBox 数据源
Use List of custom Business Objects as ComboBox datasource
我有 class 这样的:
class MyClass {
public string FirstProperty { get; set; }
public int SecondProperty { get; set; }
public MyClass(int i, string s)
{
FirstProperty = s;
SecondProperty = i;
}
}
和一个 Winform,代码如下:
List<MyClass> myList = new List<MyClass>();
myList.Add(new MyClass(1, "ABC"));
myList.Add(new MyClass(2, "ZXC"));
comboBox1.DataSource = myList;
comboBox1.ValueMember = "FirstProperty";
comboBox1.DisplayMember = "SecondProperty";
当我执行这段代码时,它出现了 "Cannot bind to new display member" 错误。
这在过去对我有用。我做错了什么?
public class MyClass
{
public string FirstProperty { get; set; }
public int SecondProperty { get; set; }
}
然后:
var myList = new List<MyClass>
{
new MyClass {SecondProperty = 1, FirstProperty = "ABC"},
new MyClass {SecondProperty = 2, FirstProperty = "ZXC"}
};
comboBox1.DataSource = myList;
comboBox1.ValueMember = "FirstProperty";
comboBox1.DisplayMember = "SecondProperty";
希望对您有所帮助。
我有 class 这样的:
class MyClass {
public string FirstProperty { get; set; }
public int SecondProperty { get; set; }
public MyClass(int i, string s)
{
FirstProperty = s;
SecondProperty = i;
}
}
和一个 Winform,代码如下:
List<MyClass> myList = new List<MyClass>();
myList.Add(new MyClass(1, "ABC"));
myList.Add(new MyClass(2, "ZXC"));
comboBox1.DataSource = myList;
comboBox1.ValueMember = "FirstProperty";
comboBox1.DisplayMember = "SecondProperty";
当我执行这段代码时,它出现了 "Cannot bind to new display member" 错误。 这在过去对我有用。我做错了什么?
public class MyClass
{
public string FirstProperty { get; set; }
public int SecondProperty { get; set; }
}
然后:
var myList = new List<MyClass>
{
new MyClass {SecondProperty = 1, FirstProperty = "ABC"},
new MyClass {SecondProperty = 2, FirstProperty = "ZXC"}
};
comboBox1.DataSource = myList;
comboBox1.ValueMember = "FirstProperty";
comboBox1.DisplayMember = "SecondProperty";
希望对您有所帮助。