c# petapoco object to datagridview, change column order -


i have bussiness object this:

[petapoco.tablename("cars")] [petapoco.primarykey("id")] public class cars : carobject {     [defaultvalue(null)]     [displayname("column name")]     public string color { get; set; }      [defaultvalue(null)]     public string engine { get; set; }      [defaultvalue(null)]     public string bhp { get; set; }      [defaultvalue(null)]     public string year { get; set; }  } 

and display in datagridview this:

list<cars> ret = db.query<cars>("select * cars").tolist(); if(ret != null)     dgv.datasource = ret; // .orderby(o => o.year).tolist();  

however, seems dgv put columns in object (design time) order. alter using loop dgv displayindex property there simple solution, attribute decorate?

thanks in advance,

ps. tried using system.componentmodel.dataannotations seems winforms isn't working. dgv isn't able bind attributes.

[display(name = "custom name", order = 2)] 

any hack? much.

thanks tim van wassenhove's elegant solution, managed wanted.

http://www.timvw.be/2007/02/04/control-the-order-of-properties-in-your-class/

it requires modified bindinglist<> class (i modified bit more)

[attributeusage(attributetargets.property)] public class propertyorderattribute : attribute {     private int order;      public propertyorderattribute(int order)     {         this.order = order;     }      public int order     {         { return this.order; }     } }  class propertyorderbindinglist<t> : bindinglist<t>, itypedlist {     public propertyorderbindinglist(list<t> list)         : base(list)     {         //     }      public propertydescriptorcollection getitemproperties(propertydescriptor[] listaccessors)     {         propertydescriptorcollection typepropertiescollection = typedescriptor.getproperties(typeof(t));         return typepropertiescollection.sort(new propertydescriptorcomparer());     }      public string getlistname(propertydescriptor[] listaccessors)     {         return string.format("a list properties {0}", typeof(t).name);     } }  class propertydescriptorcomparer : icomparer {     public int compare(object x, object y)     {         if (x == y) return 0;         if (x == null) return 1;         if (y == null) return -1;          propertydescriptor propertydescriptorx = x propertydescriptor;         propertydescriptor propertydescriptory = y propertydescriptor;          propertyorderattribute propertyorderattributex = propertydescriptorx.attributes[typeof(propertyorderattribute)] propertyorderattribute;         propertyorderattribute propertyorderattributey = propertydescriptory.attributes[typeof(propertyorderattribute)] propertyorderattribute;          if (propertyorderattributex == propertyorderattributey) return 0;         if (propertyorderattributex == null) return 1;         if (propertyorderattributey == null) return -1;          return propertyorderattributex.order.compareto(propertyorderattributey.order);     } } 

now, decorate poco object attributes order:

[defaultvalue(null)] [displayname("column name")] [propertyorder(3)] public string color { get; set; }  [defaultvalue(null)] [propertyorder(1)] public string engine { get; set; }  [defaultvalue(null)] [propertyorder(0)] public string bhp { get; set; }  [defaultvalue(null)] [propertyorder(2)] public string year { get; set; } 

and query this

list<cars> ret2 = db.query<cars>("select * cars").tolist();  propertyorderbindinglist<cars> ret = new propertyorderbindinglist<cars>(ret2); 

the properties in custom order.


Comments