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.         } 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值