DataGridView绑定BindingList<T>带数据排序的类

本文章转载:http://yuyingying1986.blog.hexun.com/30905610_d.html

 DataGridView绑定List<T>类时候,不会自动的进行排序。默认BinddingList<T> 不支持排序。

解决办法:一、手动实现DataGridView列标题的点击排序事件。二、自定义实现BinddingList<T>类 支持排序。

我们常常使用DataGridView 控件,这个控件在绑定数据源后,常常不能排序,正好我现在做的项目中也遇上了这个问题,所以上网查了一些资料,解决了这个问题,下面是我解决的方法

1.创健一个专门用来排序的类

处理手段 

  做排序处理,做本质的办法是继承ICompare接口,重新Compare方法。

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace ConcreteMonitor.VehicleManagement
{
    class ObjectPropertyCompare<T> : System.Collections.Generic.IComparer<T> 
    {

        private PropertyDescriptor _property;
        public PropertyDescriptor property
        {
            get { return _property; }
            set { _property = value; }
        }

 

        private ListSortDirection _direction;
        public ListSortDirection direction
        {
            get { return _direction; }
            set { _direction = value; }
        }

        public ObjectPropertyCompare()
        { }

        public ObjectPropertyCompare(PropertyDescriptor prop, ListSortDirection direction)
        {
            _property = prop;
            _direction = direction;
        }

        public int Compare(T x, T y)
        {

            object xValue = x.GetType().GetProperty(property.Name).GetValue(x, null);
            object yValue = y.GetType().GetProperty(property.Name).GetValue(y, null);

            int returnValue;

            if (xValue == null && yValue == null)
            {
                returnValue = 0;
            }
            else if (xValue == null)
            {
                returnValue = -1;
            }
            else if (yValue == null)
            {
                returnValue = 1;
            }
            else if (xValue is IComparable)
            {
                returnValue = ((IComparable)xValue).CompareTo(yValue);
            }
            else if (xValue.Equals(yValue))
            {
                returnValue = 0;
            }
            else
            {
                returnValue = xValue.ToString().CompareTo(yValue.ToString());
            }

            if (direction == ListSortDirection.Ascending)
            {
                return returnValue;
            }
            else
            {
                return returnValue * -1;
            }

        }
    }
}

2.创建用于绑定数据源的类

using System.Text;
using System.ComponentModel;
using JtxSignal;
using JtxSignal.Signals;
using ConcreteMonitor.Database;
using System.Data;
using System.Collections;
using ConcreteMonitor.Properties;

namespace ConcreteMonitor.VehicleManagement
{
    public class BindingCollection<T> : BindingList<T>
    {
        protected override bool SupportsSortingCore
        {
            get { return true; }
        }

        protected override bool SupportsSearchingCore
        {
            get { return true; }
        }

        private bool isSortedCore = true;
        protected override bool IsSortedCore
        {
            get
            {
                return isSortedCore;
            }
        }

        private ListSortDirection sortDirectionCore = ListSortDirection.Ascending;
        protected override ListSortDirection SortDirectionCore
        {
            get
            {
                return sortDirectionCore;
            }
        }

        private PropertyDescriptor sortPropertyCore = null;
        protected override PropertyDescriptor SortPropertyCore
        {
            get
            {
                return sortPropertyCore;
            }
        }

        protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
        {
            List<T> items = this.Items as List<T>;

            if (items != null)
            {
                ObjectPropertyCompare<T> pc = new ObjectPropertyCompare<T>(prop, direction);
                items.Sort(pc);
                isSortedCore = true;
                sortDirectionCore = direction;
                sortPropertyCore = prop;
            }
            else
            {
                isSortedCore = false;
            }

            this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
        }

        protected override void RemoveSortCore()
        {
            isSortedCore = false;
            this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
        }
    }
}


3.使用

List<T> list=new List<T>();
DataGridView.DataSource = new BindingCollection<自己定义的类>(list);

4.完成

 第二种写法:

  public class SortableBindingList<T> : BindingList<T>
    {
        private readonly Dictionary<Type, PropertyComparer<T>> comparers;
        private bool isSorted;
        private ListSortDirection listSortDirection;
        private PropertyDescriptor propertyDescriptor;

        public SortableBindingList()
            : base(new List<T>())
        {
            this.comparers = new Dictionary<Type, PropertyComparer<T>>();
        }

        public SortableBindingList(IEnumerable<T> enumeration)
            : base(new List<T>(enumeration))
        {
            this.comparers = new Dictionary<Type, PropertyComparer<T>>();
        }

        protected override bool SupportsSortingCore
        {
            get { return true; }
        }

        protected override bool IsSortedCore
        {
            get { return this.isSorted; }
        }

        protected override PropertyDescriptor SortPropertyCore
        {
            get { return this.propertyDescriptor; }
        }

        protected override ListSortDirection SortDirectionCore
        {
            get { return this.listSortDirection; }
        }

        protected override bool SupportsSearchingCore
        {
            get { return true; }
        }

        protected override void ApplySortCore(PropertyDescriptor property, ListSortDirection direction)
        {
            List<T> itemsList = (List<T>)this.Items;

            Type propertyType = property.PropertyType;
            PropertyComparer<T> comparer;
            if (!this.comparers.TryGetValue(propertyType, out comparer))
            {
                comparer = new PropertyComparer<T>(property, direction);
                this.comparers.Add(propertyType, comparer);
            }

            comparer.SetPropertyAndDirection(property, direction);
            itemsList.Sort(comparer);

            this.propertyDescriptor = property;
            this.listSortDirection = direction;
            this.isSorted = true;

            this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
        }

        protected override void RemoveSortCore()
        {
            this.isSorted = false;
            this.propertyDescriptor = base.SortPropertyCore;
            this.listSortDirection = base.SortDirectionCore;

            this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
        }

        protected override int FindCore(PropertyDescriptor property, object key)
        {
            int count = this.Count;
            for (int i = 0; i < count; ++i)
            {
                T element = this[i];
                if (property.GetValue(element).Equals(key))
                {
                    return i;
                }
            }

            return -1;
        }
    }

public class PropertyComparer<T> : IComparer<T>
    {
        private readonly IComparer comparer;
        private PropertyDescriptor propertyDescriptor;
        private int reverse;

        public PropertyComparer(PropertyDescriptor property, ListSortDirection direction)
        {
            this.propertyDescriptor = property;
            Type comparerForPropertyType = typeof(Comparer<>).MakeGenericType(property.PropertyType);
            this.comparer = (IComparer)comparerForPropertyType.InvokeMember("Default", BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.Public, null, null, null);
            this.SetListSortDirection(direction);
        }

        #region IComparer<T> Members

        public int Compare(T x, T y)
        {
            return this.reverse * this.comparer.Compare(this.propertyDescriptor.GetValue(x), this.propertyDescriptor.GetValue(y));
        }

        #endregion

        private void SetPropertyDescriptor(PropertyDescriptor descriptor)
        {
            this.propertyDescriptor = descriptor;
        }

        private void SetListSortDirection(ListSortDirection direction)
        {
            this.reverse = direction == ListSortDirection.Ascending ? 1 : -1;
        }

        public void SetPropertyAndDirection(PropertyDescriptor descriptor, ListSortDirection direction)
        {
            this.SetPropertyDescriptor(descriptor);
            this.SetListSortDirection(direction);
        }
    }

调用:   List<Model.EmailList> emails = (List<Model.EmailList>)e.Data;
        dgvEmails.DataSource =new SortableBindingList<Model.EmailList>(emails);

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏landv

从零开始学习C#——HelloWorld(一)

14850
来自专栏Java后端技术

22中编程语言的HelloWorld

8310
来自专栏大内老A

通过实例模拟ASP.NET MVC的Model绑定的机制:集合+字典

在本系列的前面两篇文章(《简单类型+复杂类型》、《数组》)我们通过创建的实例程序模拟了ASP.NET MVC默认使用的DefaultModelBinder对简单...

30870
来自专栏hbbliyong

C#基础知识回顾-- 反射(3)

获取Type对象的构造函数: 前一篇因为篇幅问题因为篇幅太短被移除首页,反射这一块还有一篇“怎样在程序集中使用反射”, 其他没有什么可以写的了,前两篇主要是铺...

30160
来自专栏前端侠2.0

学习表达式树笔记 原

文章地址:  http://www.cnblogs.com/Ninputer/archive/2009/08/28/expression_tree1.html

10520
来自专栏菩提树下的杨过

装箱与值类型虽然很容易理解,但是在实际使用中,并不总是能100%用对

public struct Point { private int m_x, m_y; public Poi...

21760
来自专栏用户3030674的专栏

Java中Json解析

首先准备一个JSON格式的字符串 * String JsonStr = "{object:{persons:" + "[{name:'呵呵',im...

43320
来自专栏技术之路

Expression 表达式树学习整理

整理了一下表达式树的一些东西,入门足够了 先从ConstantExpression 开始一步一步的来吧  它表示具有常量值的表达式 我们选建一个控制台应用程序 ...

19180
来自专栏菩提树下的杨过

Silverlight与WPF中BeginInvoke的差异

Silverlight/WPF中,如果要在多线程中对界面控件值做修改,用Dispatcher对象的BeginInvoke方法无疑是最方便的办法 ,见:温故而知新...

23780
来自专栏跟着阿笨一起玩NET

在C#中ParameterizedThreadStart和ThreadStart区别

10610

扫码关注云+社区

领取腾讯云代金券