以前不都是用table直接绑定DataGridView的,没有出现过不能排序的问题,初试List结果发现不管怎么样都不能实现排序的功能,有朋友说 DataGridView每一列都有个Sortable,默认Automatic,改成NotSortable了,结果怎样,还是不行啦。 还有朋友说, 你可以拖一个bindingsource控件. bindingsource.datasource=泛型集合 datagridview.datasource=bindingsource; 我发现也是不行,那要怎么办呢?查一下资料才知道 用泛型会失去DateTable的特性,要实现System.Collections.Generic.IComparer 才能实现排序 没有办法只能实现 一把了 看一下下面的代码吧, 基本 是这样的 代码 using System; using System.ComponentModel; using System.Collections.Generic; using System.Reflection; namespace BaseFunction { class ObjectPropertyCompare : System.Collections.Generic.IComparer { private PropertyDescriptor property; private ListSortDirection direction; public ObjectPropertyCompare(PropertyDescriptor property, ListSortDirection direction) { this.property = property; this.direction = direction; } #region IComparer /// /// 比较方法 /// /// 相对属性x /// 相对属性y /// public int Compare(T x, T y) { object xValue = x.GetType().GetProperty(property.Name).GetValue(x, null); object yValue = y.GetType().GetProperty(property.Name).GetValue(y, null); int returnValue; if (xValue is IComparable) { returnValue = ((IComparable)xValue).CompareTo(yValue); } else if (xValue.Equals(yValue)) { returnValue = 0; } else { returnValue = xValue.ToString().CompareTo(yValue.ToString()); } if (direction == ListSortDirection.Ascending) { return returnValue; } else { return returnValue * -1; } } public bool Equals(T xWord, T yWord) { return xWord.Equals(yWord); } public int GetHashCode(T obj) { return obj.GetHashCode(); } #endregion } } 在实现了这个接口之后还不能急,我们还要来写一个SortableBindingList :BindingList 的类用来绑定数据 基本实现 代码 using System; using System.ComponentModel; using System.Collections.Generic; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Text; namespace BaseFunction { public class BindingCollection : BindingList { private bool isSorted; private PropertyDescriptor sortProperty; private ListSortDirection sortDirection; protected override bool IsSortedCore { get { return isSorted; } } protected override bool SupportsSortingCore { get { return true; } } protected override ListSortDirection SortDirectionCore { get { return sortDirection; } } protected override PropertyDescriptor SortPropertyCore { get { return sortProperty; } } protected override bool SupportsSearchingCore { get { return true; } } protected override void ApplySortCore(PropertyDescriptor property, ListSortDirection direction) { List items = this.Items as List; if (items != null) { ObjectPropertyCompare pc = new ObjectPropertyCompare(property, direction); items.Sort(pc); isSorted = true; } else { isSorted = false; } sortProperty = property; sortDirection = direction; this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1)); } protected override void RemoveSortCore() { isSorted = false; this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1)); } //排序 public void Sort(PropertyDescriptor property, ListSortDirection direction) { this.ApplySortCore(property, direction); } } } 现 在应该流到怎么使用了,其实很简单 直接 BindingCollection
解决DataGridView绑定List后不能排序的问题
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
[oeasy]python019_ 如何在github仓库中进入目录_找到程序代码_找到代码
繼續運行 🥋 回憶上次內容 上上次 真寫了 萬行 代碼 這 萬行 代碼 都是寫在 明面 上的 這次 使用git命令 下載了 github上面的倉庫
原創
2024-05-30 00:35:24
一文搞懂 Spring 循环依赖
原創
2024-06-06 13:11:47
8个实用的Java Streams API
原創
2024-06-04 21:46:09
记一次疑似JVM内存泄漏的排查过程
原創
2024-06-04 12:09:32
浅析MySQL 8.0直方图原理
原創
2024-05-27 10:58:41
一文搞懂5种内存溢出案例,内含完整源码
原創
2024-06-05 10:56:55
Koupleless 单进程多应用如何解决兼容问题
原創
2024-06-04 23:14:24
pfinder实现原理揭秘
原創
2024-06-04 02:39:24
pfinder实现原理揭秘
原創
2024-06-04 02:37:09
pfinder实现原理揭秘
原創
2024-06-04 02:34:44
WebSocket替换Http协议的逻辑实现
原創
2024-06-03 11:25:33
DataCube 漏洞小结
原創
2024-05-30 11:16:40
Dolphinscheduler不重启加载Oracle驱动
原創
2024-05-28 21:22:10
鸿蒙原生应用再新丁!新华社 入局鸿蒙
原創
2024-05-27 23:42:19