1

I'm trying to create an extended version of the WinForms DataGridView (ElementDataGrid) to allow sorting and filtering. Since this will be a widget used by multiple developers, I want to hide the SortableBindingList class internally and have the user pass in just a normal List with the control creating the SortableBindingList.

I created a base class called Element, which other developers can extend, but when I set the DataPropertyName of a column to a property that isn't in Element, nothing shows up in that column. As an example, I have a Comment class that inherits from Element. I want to display the Comment Date and Comment Text in the datagrid. Neither of those columns have any data in them, but the columns using properties inherited from Element display properly.

Is there a straightforward way to have the grid display property values from classes that inherit from the Element base class? Alternately, is there a way I could have the property take a generic List?

UPDATE: Here's the method I'm using to set the data source to my SortableBindingList. As I said, properties from Element are being populated in the grid when I want to show them, but properties from Comment, which inherits from Element are not.

public List<Element> DataElements
{
  set
  {
    bindingDataSource.Clear();

    SortableBindingList<Element> boundDataSource = new SortableBindingList<Element>();
    bindingDataSource.DataSource = boundDataSource;

    foreach (Element e in value)
    {
      bindingDataSource.Add(e);
    }

    this.DataSource = bindingDataSource;
  }
}
Adam Nelson
  • 107
  • 1
  • 12

1 Answers1

1

Take a look at Marc Gravell's answer to this SO question. Assuming, as he talks about in his answer, that the data is homogeneous (meaning that you aren't mixing Comments and SomeOtherClass in your List of Element) and has at least a single element in it (so that it is able to infer the actual type of the data in the list), I think it would work for your situation.

Community
  • 1
  • 1
itsmatt
  • 31,265
  • 10
  • 100
  • 164