DataGridView 绑定List 时 排序问题的解决

  1. public   class   SortableBindingList <T>   :   BindingList <T>
  2.         {
  3.                 private   bool   isSortedCore   =   true;
  4.                 private   ListSortDirection   sortDirectionCore   =   ListSortDirection.Ascending;
  5.                 private   PropertyDescriptor   sortPropertyCore   =   null;
  6.                 private   string   defaultSortItem;
  7.                 public   SortableBindingList()   :   base()   {   }
  8.                 public   SortableBindingList(IList <T>   list)   :   base(list)   {   }
  9.                              
  10.                 protected   override   bool   SupportsSortingCore
  11.                 {
  12.                         get   {   return   true;   }
  13.                 }
  14.                 protected   override   bool   SupportsSearchingCore
  15.                 {
  16.                         get   {   return   true;   }
  17.                 }
  18.                 protected   override   bool   IsSortedCore
  19.                 {
  20.                         get   {   return   isSortedCore;   }
  21.                 }
  22.                 protected   override   ListSortDirection   SortDirectionCore
  23.                 {
  24.                         get   {   return   sortDirectionCore;   }
  25.                 }
  26.                 protected   override   PropertyDescriptor   SortPropertyCore
  27.                 {
  28.                         get   {   return   sortPropertyCore;   }
  29.                 }
  30.                 protected   override   int   FindCore(PropertyDescriptor   prop,   object   key)
  31.                 {
  32.                         for   (int   i   =   0;   i   <   this.Count;   i++)
  33.                         {
  34.                                 if   (Equals(prop.GetValue(this[i]),   key))   return   i;
  35.                         }
  36.                         return   -1;
  37.                 }
  38.                 protected   override   void   ApplySortCore(PropertyDescriptor   prop,   ListSortDirection   direction)
  39.                 {
  40.                         isSortedCore   =   true;
  41.                         sortPropertyCore   =   prop;
  42.                         sortDirectionCore   =   direction;
  43.                         Sort();
  44.                 }
  45.                 protected   override   void   RemoveSortCore()
  46.                 {
  47.                         if   (isSortedCore)
  48.                         {
  49.                                 isSortedCore   =   false;
  50.                                 sortPropertyCore   =   null;
  51.                                 sortDirectionCore   =   ListSortDirection.Ascending;
  52.                                 Sort();
  53.                         }
  54.                 }
  55.                 public   string   DefaultSortItem
  56.                 {
  57.                         get   {   return   defaultSortItem;   }
  58.                         set
  59.                         {
  60.                                 if   (defaultSortItem   !=   value)
  61.                                 {
  62.                                         defaultSortItem   =   value;
  63.                                         Sort();
  64.                                 }
  65.                         }
  66.                 }
  67.                 private   void   Sort()
  68.                 {
  69.                         List <T>   list   =   (this.Items   as   List <T> );
  70.                         list.Sort(CompareCore);
  71.                         ResetBindings();
  72.                 }
  73.                 private   int   CompareCore(T   o1,   T   o2)
  74.                 {
  75.                         int   ret   =   0;
  76.                         if   (SortPropertyCore   !=   null)
  77.                         {
  78.                                 ret   =   CompareValue(SortPropertyCore.GetValue(o1),   SortPropertyCore.GetValue(o2),   SortPropertyCore.PropertyType);
  79.                         }
  80.                         if   (ret   ==   0   &&   DefaultSortItem   !=   null)
  81.                         {
  82.                                 PropertyInfo   property   =   typeof(T).GetProperty(DefaultSortItem,   BindingFlags.Public   |   BindingFlags.GetProperty   |   BindingFlags.Instance   |   BindingFlags.IgnoreCase,   null,   null,   new   Type[0],   null);
  83.                                 if   (property   !=   null)
  84.                                 {
  85.                                         ret   =   CompareValue(property.GetValue(o1,   null),   property.GetValue(o2,   null),   property.PropertyType);
  86.                                 }
  87.                         }
  88.                         if   (SortDirectionCore   ==   ListSortDirection.Descending)   ret   =   -ret;
  89.                         return   ret;
  90.                 }
  91.                 private   static   int   CompareValue(object   o1,   object   o2,   Type   type)
  92.                 {
  93.                         //这里改成自己定义的比较
  94.                         if(o1   ==   null)return   o2==null   ?   0   :   -1;
  95.                         else   if(o2   ==   null)   return   1;
  96.                         else   if   (type.IsPrimitive   ||   type.IsEnum)   return   Convert.ToDouble(o1).CompareTo(Convert.ToDouble(o2));
  97.                         else   if   (type   ==   typeof(DateTime))   return   Convert.ToDateTime(o1).CompareTo(o2);
  98.                         else   return   String.Compare(o1.ToString().Trim(),   o2.ToString().Trim());
  99.                 }
  100.         } 

你可能感兴趣的:(list,null,equals)