DataGridView的排序和如的BindingList< T>在.NET [英] DataGridView sort and e.g. BindingList<T> in .NET

查看:302
本文介绍了DataGridView的排序和如的BindingList< T>在.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了一个的BindingList< T> 在我的Windows窗体,其中包含列表 IComparable的<联系与GT; 联系对象。现在,我想用户能够通过显示在网格中的任何列进行排序。

I'm using a BindingList<T> in my Windows Forms that contains a list of "IComparable<Contact>" Contact-objects. Now I'd like the user to be able to sort by any column displayed in the grid.

有MSDN在线上描述的方式展示了如何实现基于的BindingList℃的自定义集合; T&GT; 允许排序。但是,是不是有一个排序的事件或事情可以捕获在DataGridView(或,甚至更好,在BindingSource的),使用自定义code底层集合排序?

There is a way described on MSDN online which shows how to implement a custom collection based on BindingList<T> which allows sorting. But isn't there a Sort-event or something that could be caught in the DataGridView (or, even nicer, on the BindingSource) to sort the underlying collection using custom code?

我真的不喜欢MSDN所描述的方式。另一种方法,我可以很容易地应用LINQ查询到集合中。

I don't really like the way described by MSDN. The other way I could easily apply a LINQ query to the collection.

推荐答案

我higly AP preciate 马蒂亚斯的解决方案它的简单和美丽。

I higly appreciate Matthias' solution for its simplicity and beauty.

不过,虽然这给了优异的成绩为低数据量,大数据量工作时的表现不是那么好,由于反射。

However, while this gives excellent results for low data volumes, when working with large data volumes the performance is not so good, due to reflection.

我跑了一个测试用简单的数据对象的集合,计100000元。由一个整数类型属性排序了约1分钟。我要进一步细化的实施改变了这〜200毫秒。

I ran a test with a collection of simple data objects, counting 100000 elements. Sorting by an integer type property took around 1 min. The implementation I'm going to further detail changed this to ~200ms.

的基本思想是受益强类型的比较,同时保持ApplySortCore方法通用的。以下内容替换通用的比较与委托调用一个特定的比较器,在派生类中实现:

The basic idea is to benefit strongly typed comparison, while keeping the ApplySortCore method generic. The following replaces the generic comparison delegate with a call to a specific comparer, implemented in a derived class:

新的SortableBindingList&LT; T&GT;

New in SortableBindingList<T>:

protected abstract Comparison<T> GetComparer(PropertyDescriptor prop);

ApplySortCore更改为:

ApplySortCore changes to:

protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
{
    List<T> itemsList = (List<T>)this.Items;
    if (prop.PropertyType.GetInterface("IComparable") != null)
    {
        Comparison<T> comparer = GetComparer(prop);
        itemsList.Sort(comparer);
        if (direction == ListSortDirection.Descending)
        {
            itemsList.Reverse();
        }
    }

    isSortedValue = true;
    sortPropertyValue = prop;
    sortDirectionValue = direction;
}

现在,在派生类中的一个必须执行comparers每个可排序的属性:

Now, in the derived class one have to implement comparers for each sortable property:

class MyBindingList:SortableBindingList<DataObject>
{
        protected override Comparison<DataObject> GetComparer(PropertyDescriptor prop)
        {
            Comparison<DataObject> comparer;
            switch (prop.Name)
            {
                case "MyIntProperty":
                    comparer = new Comparison<DataObject>(delegate(DataObject x, DataObject y)
                        {
                            if (x != null)
                                if (y != null)
                                    return (x.MyIntProperty.CompareTo(y.MyIntProperty));
                                else
                                    return 1;
                            else if (y != null)
                                return -1;
                            else
                                return 0;
                        });
                    break;

                    // Implement comparers for other sortable properties here.
            }
            return comparer;
        }
    }
}

该变种需要多一点点code,但,如果性能是一个问题,我想这一点,实在值得努力的。

This variant requires a little bit more code but, if performance is an issue, I think it worths the effort.

这篇关于DataGridView的排序和如的BindingList&LT; T&GT;在.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆