zoukankan      html  css  js  c++  java
  • 记录C#控件DataGridView绑定BindingList无法排序问题(转)

    本文在网络上搜集到的资料,自己改了一下类名而已,致谢。

    public class FlexibleBindingList<T> : BindingList<T> {
        private bool isSortedCore = true;
        private ListSortDirection sortDirectionCore = ListSortDirection.Ascending;
        private PropertyDescriptor sortPropertyCore = null;
        private string defaultSortItem;
        public FlexibleBindingList() : base() { }
        public FlexibleBindingList(IList<T> list) : base(list) { }
    
        protected override bool SupportsSortingCore {
            get { return true; }
        }
        protected override bool SupportsSearchingCore {
            get { return true; }
        }
        protected override bool IsSortedCore {
            get { return isSortedCore; }
        }
        protected override ListSortDirection SortDirectionCore {
            get { return sortDirectionCore; }
        }
        protected override PropertyDescriptor SortPropertyCore {
            get { return sortPropertyCore; }
        }
        protected override int FindCore(PropertyDescriptor prop, object key) {
            for (int i = 0; i < this.Count; i++) {
                if (Equals(prop.GetValue(this[i]), key)) return i;
            }
            return -1;
        }
        protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction) {
            isSortedCore = true;
            sortPropertyCore = prop;
            sortDirectionCore = direction;
            Sort();
        }
        protected override void RemoveSortCore() {
            if (isSortedCore) {
                isSortedCore = false;
                sortPropertyCore = null;
                sortDirectionCore = ListSortDirection.Ascending;
                Sort();
            }
        }
        public string DefaultSortItem {
            get { return defaultSortItem; }
            set {
                if (defaultSortItem != value) {
                    defaultSortItem = value;
                    Sort();
                }
            }
        }
        private void Sort() {
            List<T> list = (this.Items as List<T>);
            list.Sort(CompareCore);
            ResetBindings();
        }
        private int CompareCore(T o1, T o2) {
            int ret = 0;
            if (SortPropertyCore != null) {
                ret = CompareValue(SortPropertyCore.GetValue(o1), SortPropertyCore.GetValue(o2), SortPropertyCore.PropertyType);
            }
            if (ret == 0 && DefaultSortItem != null) {
                PropertyInfo property = typeof(T).GetProperty(DefaultSortItem, BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.IgnoreCase, null, null, new Type[0], null);
                if (property != null) {
                    ret = CompareValue(property.GetValue(o1, null), property.GetValue(o2, null), property.PropertyType);
                }
            }
            if (SortDirectionCore == ListSortDirection.Descending) ret = -ret;
            return ret;
        }
        private static int CompareValue(object o1, object o2, Type type) {
            //这里改成自己定义的比较
            if (o1 == null) return o2 == null ? 0 : -1;
            else if (o2 == null) return 1;
            else if (type.IsPrimitive || type.IsEnum) return Convert.ToDouble(o1).CompareTo(Convert.ToDouble(o2));
            else if (type == typeof(DateTime)) return Convert.ToDateTime(o1).CompareTo(o2);
            else return String.Compare(o1.ToString().Trim(), o2.ToString().Trim());
        }
    }
  • 相关阅读:
    scikit-learn机器学习(二)逻辑回归进行二分类(垃圾邮件分类),二分类性能指标,画ROC曲线,计算acc,recall,presicion,f1
    scikit-learn机器学习(一)简单线性回归
    Python描述性统计numpy
    Python统计分析可视化库seaborn(相关性图,变量分布图,箱线图等等)
    Introduction to statistical learning:with Applications in R (书,数据,R代码,链接)
    用python做线性规划
    Python-sympy科学计算与数据处理(求极限及其它功能)
    Python-sympy科学计算与数据处理(方程,微分,微分方程,积分)
    Python-sympy科学计算与数据处理(数学表达式)
    Effective JavaScript Item 55 接受配置对象作为函数參数
  • 原文地址:https://www.cnblogs.com/sam-snow-v/p/14334121.html
Copyright © 2011-2022 走看看