我有这样的课程:
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";
当我执行此代码时,它会出现“无法绑定到新显示成员”的错误。 这在过去对我有用。我做错了什么?
答案 0 :(得分:1)
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";
我希望它有所帮助。