此代码会导致DataGridView grid
显示空行,尽管它有一列DataPropertyName
设置为“MyProp1”:
public class MyClass
{
public int MyProp1;
public int MyProp2;
public int MyProp3;
}
public class MyItems:IListSource
{
BindingList<MyClass> _items = new BindingList<MyClass>();
//..............................
//IListSource
public bool ContainsListCollection
{
get { return false; }
}
//IListSource
public System.Collections.IList GetList()
{
return _items;
}
}
MyItems i = new MyItems();
.............
//MyItems list is populated
.............
grid.DataSource = i;
可能出现什么问题?
如果我使用“MyProp1”列创建一个DataTable,其内容将以正确的方式显示。
答案 0 :(得分:7)
您需要将MyClass
的公共字段更改为相应的属性:
public class MyClass
{
public int MyProp1 { get; set; }
public int MyProp2 { get; set; }
public int MyProp3 { get; set; }
}