NET--可绑定可排序列表
/// <summary> /// BindingList with sorting feature /// </summary> /// <typeparam name="T"></typeparam> public class SortableBindingList<T> : BindingList<T> { private bool isSortedCore = true ; private ListSortDirection sortDirectionCore = ListSortDirection.Ascending; private PropertyDescriptor sortPropertyCore = null ; private string defaultSortItem; /// <summary> /// constructor /// </summary> public SortableBindingList() : base () { } /// <summary> /// constructor /// </summary> /// <param name="list"></param> 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(); } } /// <summary> /// Default sort field /// </summary> 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; if (o2 == null ) return 1; if (type.IsPrimitive || type.IsEnum || IsNullablePrimitiveType(type)) return Convert.ToDouble(o1).CompareTo(Convert.ToDouble(o2)); if (type == typeof (DateTime) || type == typeof (DateTime?)) return Convert.ToDateTime(o1).CompareTo(o2); return String.Compare(o1.ToString().Trim(), o2.ToString().Trim()); } /// <summary> /// 判断是否为基元类型可空类型 /// </summary> /// <param name="type">类型</param> /// <returns>是否</returns> public static bool IsNullablePrimitiveType(Type type) { if (!type.IsGenericType) { return false ; } //泛型的基础类型是否是基元类型 if (!type.GetGenericArguments()[0].IsPrimitive) { return false ; } Type genericTypeDefinition = type.GetGenericTypeDefinition(); return genericTypeDefinition != null && genericTypeDefinition.Equals( typeof (Nullable<>)); } } |
用法:
dgvDetail.DataSource= new SortableBindingList<Person>(listPerson);
作者:imap
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架