c# - Forestall duplicated columns when add TypeDescriptor Provider's -
i'm trying add typedescriptor provider class use how datasource in datagridview, code:
private void frmtarifadovariantes_load(object sender, eventargs e) { system.componentmodel.typedescriptor.addprovider((new mytypedescriptionprovider<tarifadovariantebe>()), typeof(tarifadobloquesbe)); system.componentmodel.typedescriptor.addprovider((new mytypedescriptionprovider<bloqueprendabe>()), typeof(tarifadobloquesbe)); }
question 1:
first time good, when closed form , open again, data provider descriptor twice columns, tried remove first providers , add again, doesn't work.
there mytypedescriptionprovider, found searching on web:
public class mytypedescriptionprovider<t> : typedescriptionprovider { private icustomtypedescriptor td; public mytypedescriptionprovider() : this(typedescriptor.getprovider(typeof(tarifadobloquesbe))) { } public mytypedescriptionprovider(typedescriptionprovider parent) : base(parent) { } public override icustomtypedescriptor gettypedescriptor(type objecttype, object instance) { if (td == null) { td = base.gettypedescriptor(objecttype, instance); td = new mycustomtypedescriptor(td, typeof(t)); } return td; } } public class subpropertydescriptor : propertydescriptor { private propertydescriptor _subpd; private propertydescriptor _parentpd; public subpropertydescriptor(propertydescriptor parentpd, propertydescriptor subpd, string pdname) : base(pdname, null) { _subpd = subpd; _parentpd = parentpd; } public override bool isreadonly { { return false; } } public override void resetvalue(object component) { } public override bool canresetvalue(object component) { return false; } public override bool shouldserializevalue(object component) { return true; } public override type componenttype { { return _parentpd.componenttype; } } public override type propertytype { { return _subpd.propertytype; } } public override object getvalue(object component) { return _subpd.getvalue(_parentpd.getvalue(component)); } public override void setvalue(object component, object value) { _subpd.setvalue(_parentpd.getvalue(component), value); onvaluechanged(component, eventargs.empty); } } public class mycustomtypedescriptor : customtypedescriptor { type typeproperty; public mycustomtypedescriptor(icustomtypedescriptor parent, type type) : base(parent) { typeproperty = type; } public override propertydescriptorcollection getproperties(attribute[] attributes) { propertydescriptorcollection cols = base.getproperties(attributes); string propertyname = ""; foreach (propertydescriptor col in cols) { //se agregó si el attributo es browsable if (col.isbrowsable) { if (col.propertytype.name == typeproperty.name) { propertyname = col.name; break; } } } propertydescriptor pd = cols[propertyname]; propertydescriptorcollection children = pd.getchildproperties(); propertydescriptor[] array = new propertydescriptor[cols.count + children.count]; int count = cols.count; cols.copyto(array, 0); foreach (propertydescriptor cpd in children) { if (cpd.isbrowsable) { array[count] = new subpropertydescriptor(pd, cpd, pd.name + "_" + cpd.name); count++; } } //propertydescriptor[] arraytmp = new propertydescriptor[count]; array.resize(ref array, count); propertydescriptorcollection newcols = new propertydescriptorcollection(array); return newcols; } }
question 2:
how can make mytypedescriptionprovider dinamyc, mean if can check, hard code class (tarifadobloquesbe) of getprovider, in part:
private icustomtypedescriptor td; public mytypedescriptionprovider() : this(typedescriptor.getprovider(typeof(tarifadobloquesbe))) { }
i want allow sending class provider. thank you.
i attached 2 images, first 1 ok, next 1 can check twice columns, seeing scroll bar.
all here
duplicate columns
Comments
Post a Comment