zoukankan      html  css  js  c++  java
  • Winform DataGridView扩展

    public class ObjectPropertyCompare<T> : IComparer<T>
    {
        private readonly PropertyDescriptor property;
        private readonly ListSortDirection direction;
    
        public ObjectPropertyCompare(PropertyDescriptor property, ListSortDirection direction)
        {
            this.property = property;
            this.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 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;
            }
            return returnValue * -1;
        }
    }
    public class BindingCollection<T> : BindingList<T>
    {
        private bool isSorted;
        private PropertyDescriptor sortProperty;
        private ListSortDirection sortDirection;
    
        public BindingCollection()
        {
        }
    
        public BindingCollection(IList<T> list)
            : base(list)
        {
        }
    
        protected override void ApplySortCore(PropertyDescriptor property, ListSortDirection direction)
        {
            var items = Items as List<T>;
    
            if (items != null)
            {
                var pc = new ObjectPropertyCompare<T>(property, direction);
                try
                {
                    items.Sort(pc);
                    isSorted = true;
                }
                catch (Exception)
                {
                    isSorted = false;
                }
            }
            else
            {
                isSorted = false;
            }
    
            sortProperty = property;
            sortDirection = direction;
    
            OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
        }
    
        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 void RemoveSortCore()
        {
            isSorted = false;
            OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
        }
    }
  • 相关阅读:
    笔试题 输出金字塔 面试经典
    C++ 函数, 虚函数, 纯虚函数
    EJB 根据beanName引用EJB
    【J2EE性能分析篇】JVM参数对J2EE性能优化的影响【转】
    C++ 引用和指针作为函数参数的例子。请不要拍砖
    lucene 总结
    二维数组按列序号排序 面试经典
    http://www.linuxidc.com/Linux/201004/25494.htm
    银行取款费用
    PHP 生成 csv 文件时乱码解决
  • 原文地址:https://www.cnblogs.com/yao2yao4/p/3179134.html
Copyright © 2011-2022 走看看