I am using .NET 3.5
I have a class Member that inherits from an abstract class Person
public class Member : Person { public int Number { get; set; } public string LastName { get; set; } public string OtherNames { get; set; } }
I have declared a BindingList as follows:
BindingList<Person> p_List = new BindingList<Person>();
I have a DataGridView with columns as follows:
col1.DataPropertyName = "Number"; col2.DataPropertyName = "LastName"; col3.DataPropertyName = "OtherNames";
I populate the binding list in 2 from a list of Member objects. I then bind the BindingList to the datagridview as follows:
this.dgListView.DataSource = p_List;
The datagridview has 3 columns and as many rows as the number of Members. However, the cells do not have any data. I think the reason is because the 3 properties in Member are not available to the parent (Person). When I create a "Member-Typed" BindingList m_List and then populate it by looping througb the "Person-Typed" p_List, the datagridview populates without a problem. (see code below).
BindingList<Member> m_List = new BindingList<Member>();
foreach (Person p in p_Plist)
{
Member m = (Member)p;
m_List.Add(m);
}
this.dgListView.DataSource = m_List;
However, I want to avoid this because of a number of reasons. The key reason is that p_List is a property in a class that I am using to access different objects of type Person. Each of these objects is associated with a different windows form. I therefore want to use p_List to display the object in the relevant form, as long as the object class inherits from Person class. I also do not want to maintain 2 copies (m_List and p_List) because if I sort, add, delete or filter from one, i will be forced to sort on the other. Is there a way of making the datagridview detect the properties in BindingList<Person>
hence be able to populate it as if it was bound to BindingList<Member>
?