C# DateGridView解决绑定数据源无法排序

第一步:创建用于排序帮助类

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;

namespace ZB.QueueSys.Common
{
    public class DgvSortHelper<T> : BindingList<T>
    {
        private bool isSortedCore = true;
        private ListSortDirection sortDirectionCore = ListSortDirection.Ascending;
        private PropertyDescriptor sortPropertyCore = null;
        private string defaultSortItem;

        public DgvSortHelper() : base() { }

        public DgvSortHelper(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());
        }
    }
}  

第二步:程序设置,注,dataSource为数据集

 DgvSortHelper<StudentInfo> temps = new DgvSortHelper<StudentInfo>();
            foreach (StudentInfo item in dataSource)
            {
                temps.Add(item);
            }

            this.dgvList.DataSource = temps;

  重构一下

 public DgvSortHelper<T> GetSorDataSource<T>(List<T> list)
        {
            DgvSortHelper<T> temps = new DgvSortHelper<T>();
            foreach (T item in list)
            {
                temps.Add(item);
            }
            return temps;
        }

this.dgvList.DataSource = GetSorDataSource(dataSource);

 方式二:根据数据集动态生成行信息

  private void InitDgvList(List<T> queueList)
        {
            this.dgvList.Rows.Clear();//先清空dgv
            if (queueList == null || queueList.Count == 0) return;
            foreach (T queue in queueList)
            {
                try
                {   //处理需要使用的所有列                
                    row.Cells["LODGESECTIONCODE"].Value = queue.LODGESECTIONCODE;              
                }
                catch (Exception ex) { }
            }
        }

  

 

博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!
原文地址:https://www.cnblogs.com/YYkun/p/14765076.html