c# - Make DataGridView Show Properties of Derived Class Objects when Using List of Base Class Objects -


is there way have datagridview show properties of derived class if members held in list of base class type, in properties don't exist? have following:

public class mybaseclass { public int mypropertybase { get; set; } //... }  public class myderivedclass : mybaseclass { public int mypropertyderived { get; set; } //... }  // create list of type mybaseclass, holding myderivedclass objects list<mybaseclass> list = new list<mybaseclass>(); list.add(new myderivedclass(1)); list.add(new myderivedclass(2)); list.add(new myderivedclass(3));  // declare datagridview datagridview datagridview = new datagridview(); datagridview.autogeneratecolumns = false;  // create display column datagridview datagridviewtextboxcolumn col = new datagridviewtextboxcolumn(); 

when bind list datagridview , show mypropertybase property works fine:

// select mypropertybase display in column col.datapropertyname = "mypropertybase"; datagridview.columns.add(col); datagridview.datasource = list; 

however, if instead want show mypropertyderived using same list<mybaseclass> won't work , shows empty cells (although shows number of cells corresponding items in list):

// select mypropertyderived display in column col.datapropertyname = "mypropertyderived"; datagridview.columns.add(col);     datagridview.datasource = list; 

i need keep list typed abstract base class since used elsewhere in code on place. there way expose derived class properties datagridview if exist , display , empty cell if property doesn't exist? helpful have each of derived classes expose (through attributes perhaps) display when bound datagridview , have datagridview fill in list.

one way cast each element in list,do next change , 1,2,3 in datagridview:

col.datapropertyname = "mypropertyderived"; datagridview.columns.add(col); datagridview.datasource = list.convertall(c => c myderivedclass); 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -