1
votes

I am using .NET 3.5

  1. 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; }
    }
    
  2. I have declared a BindingList as follows:

    BindingList<Person> p_List = new BindingList<Person>();
    
  3. I have a DataGridView with columns as follows:

    col1.DataPropertyName = "Number";
    col2.DataPropertyName = "LastName";
    col3.DataPropertyName = "OtherNames";
    
  4. 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>?

1
I presume in point three there is a type as you're setting the datapropertyname for ONLY column1. (should be col1,col2,col3)Quinton Bernhardt
Sorry, it was one of these copy-paste errors. its actually col1, col2, col3. I have corrected.gkinu

1 Answers

0
votes

Firstly those properties should be defined in the Person class. If you want to override them in the Member class you have to make the properties in the Person class virtual, and then use the override modifier when overriding them in Member.

        public class Person {
            public virtual int Number { get; set; }
            public virtual string LastName { get; set; }
            public virtual string OtherNames { get; set; }
        }

        public class Member : Person {
            public override int Number { get; set; }
            public override string LastName { get; set; }
            public override string OtherNames { get; set; }
        }

Polymorphism isn't required though if all your Person derived entities share the properties Number, LastName and OtherNames... you can just set their values... just saying.