DataGridView使用非泛型或者未实现IBindingList接口泛型集合的列排序问题
2015-06-25 17:29
671 查看
今天看到了一篇很好的文章DataGridView使用非泛型或者未实现IBindingList接口泛型集合的列排序问题,分享一下。
在需要的地方直接new一个就OK了.
转载来源三十岁,原创地址:http://blog.30c.org/2561.html
/// <summary> /// 可排序绑定用泛型列表,还是叫起来别扭... /// Author:Clove /// Site:http://30c.org /// </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; 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) { 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()); } }
在需要的地方直接new一个就OK了.
private SortableBindingList<MyProcess> myProcessList = new SortableBindingList<MyProcess>();
转载来源三十岁,原创地址:http://blog.30c.org/2561.html
相关文章推荐
- linxu php连接sqlserver
- PHPexcel
- STM8S之时钟设置
- Linux输入子系统(Input Subsystem)
- .Net 与 Javascript 混合编程系列
- 插件开发--BE插件开发
- iOS Png Crush 错误
- Windows 7 IIS7.5上部署MVC实例
- SAT历年真题之:作文题目汇总(五)
- SpringMVC处理Date类型的属性的方法
- 如何去除重复数据?
- 12864液晶 介绍
- 将鼠标位置转换成世界坐标
- LintCode Two Strings Are Anagrams 两个字符串是变位词
- APP被苹果APPStore拒绝的各种原因
- createEigenFaceRecognizer
- 网康科技一处配置不当导致可内网渗透 --- Linux udf提权
- 06 序列(笔记)
- Android应用程序与SurfaceFlinger服务之间的共享UI元数据(SharedClient)的创建过程分析 .
- APP被苹果APPStore拒绝的各种原因 分类: ios相关 app相关 2015-06-25 17:27 200人阅读 评论(0) 收藏