繁体   English   中英

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

[英]c# petapoco object to datagridview, change column order

我有一个这样的业务对象:

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

有骇客吗? 非常感谢。

感谢Tim Van 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);

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM