简体   繁体   中英

DataGridView bound to BindingList displays empty rows

This code causes DataGridView grid to display empty rows although it has a column with DataPropertyName set to "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;

What could be wrong?

If I make a DataTable with "MyProp1" column, its contents is displayed the right way.

You need to change the public fields of MyClass to corresponding properties:

public class MyClass
{
   public int MyProp1 { get; set; }
   public int MyProp2 { get; set; }
   public int MyProp3 { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM