List<T>绑定到datagridview时不能排序

在把List<T>绑定到datagridview时不能排序。需要实现Ibindinglist接口。但这个接口层层引用了N多虚类。需要第一个类去实现虚类的方法。比较麻烦。在有网友提供了一个解决方案。经测试,完美支持。感谢这位网友。

namespace BaseFunction
{
    class ObjectPropertyCompare<T> : System.Collections.Generic.IComparer<T>
    {
        private PropertyDescriptor property;
        private ListSortDirection direction;


        public ObjectPropertyCompare(PropertyDescriptor property, ListSortDirection direction)
        {
            this.property = property;
            this.direction = direction;
        }


        #region IComparer<T>


        /// <summary>
        /// 比较方法
        /// </summary>
        /// <param name="x">相对属性x</param>
        /// <param name="y">相对属性y</param>
        /// <returns></returns>
        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 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;
            }
        }


        public bool Equals(T xWord, T yWord)
        {
            return xWord.Equals(yWord);
        }


        public int GetHashCode(T obj)
        {
            return obj.GetHashCode();
        }


        #endregion
    }
    public class BindingCollection<T> : BindingList<T>
    {
        private bool isSorted;
        private PropertyDescriptor sortProperty;
        private ListSortDirection sortDirection;


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


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


        protected override ListSortDirection SortDirectionCore
        {
            get { return sortDirection; }
        }


        protected override PropertyDescriptor SortPropertyCore
        {
            get { return sortProperty; }
        }


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


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


            if (items != null)
            {
                ObjectPropertyCompare<T> pc = new ObjectPropertyCompare<T>(property, direction);
                items.Sort(pc);
                isSorted = true;
            }
            else
            {
                isSorted = false;
            }


            sortProperty = property;
            sortDirection = direction;


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


        protected override void RemoveSortCore()
        {
            isSorted = false;
            this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
        }
        //排序
        public void Sort(PropertyDescriptor property, ListSortDirection direction)
        {
            this.ApplySortCore(property, direction);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值