DataGridView DataSource 如何实现排序
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                DataGridView DataSource 如何实现排序
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.                        
                                將數(shù)據(jù)綁定在下面的類中就可以實(shí)現(xiàn)排序
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; }}}轉(zhuǎn)載于:https://www.cnblogs.com/norsd/p/6359308.html
總結(jié)
以上是生活随笔為你收集整理的DataGridView DataSource 如何实现排序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
                            
                        - 上一篇: HiveQL与SQL区别
 - 下一篇: mysql 备份脚本