zoukankan      html  css  js  c++  java
  • C# 实现DataGridView自动排序

    新建类

        public class SortableBindingList<T> : BindingList<T>
        {
            private bool isSortedCore = true;
            private ListSortDirection sortDirectionCore = ListSortDirection.Ascending;
            private PropertyDescriptor sortPropertyCore = null;
            private string defaultSortItem;
    
            public SortableBindingList() : base() { }
    
            public SortableBindingList(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)
            {
                
                //MessageBox.Show(type.GetType().ToString());
                if (o1 == null) return o2 == null ? 0 : -1;
                if (o2 == null) return 1;
                if (type.ToString().Contains("Int")) return Convert.ToDouble(o1).CompareTo(Convert.ToDouble(o2));//注意这里if (type.IsPrimitive || type.IsEnum)是取不到Int型的
                if (type == typeof(DateTime)) return Convert.ToDateTime(o1).CompareTo(o2);
                return String.Compare(o1.ToString().Trim(), o2.ToString().Trim());
            }
      使用:(直接把查询出来的List<>扔进去即可)
    dataGridView1.DataSource = new App_Code.SortableBindingList<App_Code.WordCount>(new App_Code.WorldCountDA().SelectAll());
    
    

    还没试过日期

     
  • 相关阅读:
    html/css 滚动到元素位置,显示加载动画
    React 监听页面滚动,界面动态显示
    Html/css 列表项 区分列表首尾
    Html/css 水平布局居中
    Html 设置标题栏顶部固定
    TypeScript 引用资源文件后提示找不到的异常处理
    Github自动打包并推送Nuget版本
    获取电脑的网络连接状态(六)适配器状态 及 几种方案耗时对比
    获取电脑的网络连接状态(五)WebClient
    获取电脑的网络连接状态(四)IPHost
  • 原文地址:https://www.cnblogs.com/ck235/p/5081154.html
Copyright © 2011-2022 走看看