DataGridView DataSource 如何实现排序
生活随笔
收集整理的這篇文章主要介紹了
DataGridView DataSource 如何实现排序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
將數據綁定在下面的類中就可以實現排序
public class SortableBindingList<T> : BindingList<T>{private ArrayList sortedList;private bool isSortedValue;public SortableBindingList(){}public SortableBindingList(IList<T> list){foreach (object o in list){this.Add((T)o);}}protected override bool SupportsSortingCore{get { return true; }}protected override bool IsSortedCore{get { return isSortedValue; }}ListSortDirection sortDirectionValue;PropertyDescriptor sortPropertyValue;protected override void ApplySortCore(PropertyDescriptor prop,ListSortDirection direction){sortedList = new ArrayList();Type interfaceType = prop.PropertyType.GetInterface("IComparable");if (interfaceType == null && prop.PropertyType.IsValueType){Type underlyingType = Nullable.GetUnderlyingType(prop.PropertyType);if (underlyingType != null){interfaceType = underlyingType.GetInterface("IComparable");}}if (interfaceType != null){sortPropertyValue = prop;sortDirectionValue = direction;IEnumerable<T> query = base.Items;if (direction == ListSortDirection.Ascending){query = query.OrderBy(i => prop.GetValue(i));}else{query = query.OrderByDescending(i => prop.GetValue(i));}int newIndex = 0;foreach (object item in query){this.Items[newIndex] = (T)item;newIndex++;}isSortedValue = true;this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));}else{throw new NotSupportedException("Cannot sort by " + prop.Name +". This" + prop.PropertyType.ToString() +" does not implement IComparable");}}protected override PropertyDescriptor SortPropertyCore{get { return sortPropertyValue; }}protected override ListSortDirection SortDirectionCore{get { return sortDirectionValue; }}}轉載于:https://www.cnblogs.com/norsd/p/6359308.html
總結
以上是生活随笔為你收集整理的DataGridView DataSource 如何实现排序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HiveQL与SQL区别
- 下一篇: Unity3d通用工具类之生成文件的MD