原文地址:C#中DataGridView使用非泛型或者未实现IBindingList接口泛型集合的列排序问题   

 

都说标题长了才有霸气,咱这个标题够霸气了吧.读的我自己都晕乎乎的.今天遇到一个电脑操作问题,没找到合适的软件就自己写了一个.结果遇到了标题中出现的问题.DataGridView中的数据说什么就是排序不了,折腾了好一顿才发现是IBindingList接口问题.出个方案放在这吧,阿门.

  1 /// <summary>
  2 /// 可排序绑定用泛型列表,还是叫起来别扭...
  3 /// Author:Clove
  4 /// Site:http://30c.org
  5 /// </summary>
  6 /// <typeparam name="T"></typeparam>
  7 public class SortableBindingList<T> : BindingList<T>
  8 {
  9 private bool isSortedCore = true;
 10 private ListSortDirection sortDirectionCore = ListSortDirection.Ascending;
 11 private PropertyDescriptor sortPropertyCore = null;
 12 private string defaultSortItem;
 13 
 14 public SortableBindingList() : base() { }
 15 
 16 public SortableBindingList(IList<T> list) : base(list) { }
 17 
 18 protected override bool SupportsSortingCore
 19 {
 20     get { return true; }
 21 }
 22 
 23 protected override bool SupportsSearchingCore
 24 {
 25     get { return true; }
 26 }
 27 
 28 protected override bool IsSortedCore
 29 {
 30     get { return isSortedCore; }
 31 }
 32 
 33 protected override ListSortDirection SortDirectionCore
 34 {
 35     get { return sortDirectionCore; }
 36 }
 37 
 38 protected override PropertyDescriptor SortPropertyCore
 39 {
 40     get { return sortPropertyCore; }
 41 }
 42 
 43 protected override int FindCore(PropertyDescriptor prop, object key)
 44 {
 45     for (int i = 0; i < this.Count; i++)
 46     {
 47     if (Equals(prop.GetValue(this[i]), key)) return i;
 48     }
 49     return -1;
 50 }
 51 
 52 protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
 53 {
 54     isSortedCore = true;
 55     sortPropertyCore = prop;
 56     sortDirectionCore = direction;
 57     Sort();
 58 }
 59 
 60 protected override void RemoveSortCore()
 61 {
 62     if (isSortedCore)
 63     {
 64     isSortedCore = false;
 65     sortPropertyCore = null;
 66     sortDirectionCore = ListSortDirection.Ascending;
 67     Sort();
 68     }
 69 }
 70 
 71 public string DefaultSortItem
 72 {
 73     get { return defaultSortItem; }
 74     set
 75     {
 76     if (defaultSortItem != value)
 77     {
 78         defaultSortItem = value;
 79         Sort();
 80     }
 81     }
 82 }
 83 
 84 private void Sort()
 85 {
 86     List<T> list = (this.Items as List<T>);
 87     list.Sort(CompareCore);
 88     ResetBindings();
 89 }
 90 
 91 private int CompareCore(T o1, T o2)
 92 {
 93     int ret = 0;
 94     if (SortPropertyCore != null)
 95     {
 96     ret = CompareValue(SortPropertyCore.GetValue(o1), SortPropertyCore.GetValue(o2), SortPropertyCore.PropertyType);
 97     }
 98     if (ret == 0 && DefaultSortItem != null)
 99     {
100     PropertyInfo property = typeof(T).GetProperty(DefaultSortItem, BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.IgnoreCase, null, null, new Type[0], null);
101     if (property != null)
102     {
103         ret = CompareValue(property.GetValue(o1, null), property.GetValue(o2, null), property.PropertyType);
104     }
105     }
106     if (SortDirectionCore == ListSortDirection.Descending) ret = -ret;
107     return ret;
108 }
109 
110 private static int CompareValue(object o1, object o2, Type type)
111 {
112     if (o1 == null) return o2 == null ? 0 : -1;
113     else if (o2 == null) return 1;
114     else if (type.IsPrimitive || type.IsEnum) return Convert.ToDouble(o1).CompareTo(Convert.ToDouble(o2));
115     else if (type == typeof(DateTime)) return Convert.ToDateTime(o1).CompareTo(o2);
116     else return String.Compare(o1.ToString().Trim(), o2.ToString().Trim());
117 }
118 }

 

在需要的地方直接new一个就OK了.

1 private SortableBindingList<MyProcess> myProcessList = new SortableBindingList<MMyProcess>;