绑定到BindingList的DataGridView显示空行

Pau*_*aul 1 c# data-binding datagridview bindinglist

此代码导致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;
Run Code Online (Sandbox Code Playgroud)

可能有什么不对?

如果我使用"MyProp1"列创建一个DataTable,其内容将以正确的方式显示.

Ali*_*eza 7

您需要将公共字段更改MyClass为相应的属性:

public class MyClass
{
   public int MyProp1 { get; set; }
   public int MyProp2 { get; set; }
   public int MyProp3 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)