Search code examples
c#data-bindingdatagridviewbindinglist

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.


Solution

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

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