c#petapoco对象到datagridview,更改列顺序

时间:2015-06-03 08:21:02

标签: c# 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)]

任何黑客攻击?非常感谢。

1 个答案:

答案 0 :(得分:1)

感谢 Tim Van Wassenhove 优雅的解决方案,我设法做到了我想要的。

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

它需要修改的BindingList&lt;&gt; class(我修改了一点)

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

属性将按自定义顺序排列。

相关问题