2015-06-03 92 views
1

我有一個經營業務對象是這樣的:C#petapoco對象datagridview的,更改列順序

[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; } 

} 

和我的DataGridView這樣顯示:

List<Cars> ret = db.Query<Cars>("select * from cars").ToList(); 
if(ret != null) 
    dgv.DataSource = ret; // .OrderBy(o => o.Year).ToList(); 

然而,似乎DGV把列狀按對象(設計時間)順序。我可以通過使用具有DGV DisplayIndex屬性的循環來改變這一點,但是有一個簡單的解決方案,一些屬性裝飾?

在此先感謝,

PS。我也嘗試使用System.ComponentModel.DataAnnotations,但似乎WinForms不起作用。 DGV無法綁定該屬性。

[Display(Name = "Custom Name", Order = 2)] 

任何破解?非常感謝。

+0

您可以使用您的訂單在設計器中創建列並設置AutoGenerateColumns = false; – Fabio

回答

1

感謝蒂凡凡Wassenhove的優雅的解決方案,我設法做到我想要的。

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

它需要修改的BindingList <>類(我修改多一點)

[AttributeUsage(AttributeTargets.Property)] 
public class PropertyOrderAttribute : Attribute 
{ 
    private int order; 

    public PropertyOrderAttribute(int order) 
    { 
     this.order = order; 
    } 

    public int Order 
    { 
     get { 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 with Properties for {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 as PropertyDescriptor; 
     PropertyDescriptor propertyDescriptorY = y as PropertyDescriptor; 

     PropertyOrderAttribute propertyOrderAttributeX = propertyDescriptorX.Attributes[typeof(PropertyOrderAttribute)] as PropertyOrderAttribute; 
     PropertyOrderAttribute propertyOrderAttributeY = propertyDescriptorY.Attributes[typeof(PropertyOrderAttribute)] as PropertyOrderAttribute; 

     if (propertyOrderAttributeX == propertyOrderAttributeY) return 0; 
     if (propertyOrderAttributeX == null) return 1; 
     if (propertyOrderAttributeY == null) return -1; 

     return propertyOrderAttributeX.Order.CompareTo(propertyOrderAttributeY.Order); 
    } 
} 

現在,只是裝飾用的順序POCO對象屬性:

[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; } 

而且像這樣的查詢

List<Cars> ret2 = db.Query<Cars>("select * from cars").ToList(); 

PropertyOrderBindingList<Cars> ret = new PropertyOrderBindingList<Cars>(ret2); 

屬性將按照自定義順序排列。