c# winform支持容器排序


ExpandedBlockStart.gif SortableBindingList
public   class  SortableBindingList < T >  : BindingList < T >
    {
        
private   readonly  Dictionary < Type, PropertyComparer < T >>  comparers;
        
private   bool  isSorted;
        
private  ListSortDirection listSortDirection;
        
private  PropertyDescriptor propertyDescriptor;

        
public  SortableBindingList()
            : 
base ( new  List < T > ())
        {
            
this .comparers  =   new  Dictionary < Type, PropertyComparer < T >> ();
        }

        
public  SortableBindingList(IEnumerable < T >  enumeration)
            : 
base ( new  List < T > (enumeration))
        {
            
this .comparers  =   new  Dictionary < Type, PropertyComparer < T >> ();
        }

        
protected   override   bool  SupportsSortingCore
        {
            
get  {  return   true ; }
        }

        
protected   override   bool  IsSortedCore
        {
            
get  {  return   this .isSorted; }
        }

        
protected   override  PropertyDescriptor SortPropertyCore
        {
            
get  {  return   this .propertyDescriptor; }
        }

        
protected   override  ListSortDirection SortDirectionCore
        {
            
get  {  return   this .listSortDirection; }
        }

        
protected   override   bool  SupportsSearchingCore
        {
            
get  {  return   true ; }
        }

        
protected   override   void  ApplySortCore(PropertyDescriptor property, ListSortDirection direction)
        {
            List
< T >  itemsList  =  (List < T > ) this .Items;

            Type propertyType 
=  property.PropertyType;
            PropertyComparer
< T >  comparer;
            
if  ( ! this .comparers.TryGetValue(propertyType,  out  comparer))
            {
                comparer 
=   new  PropertyComparer < T > (property, direction);
                
this .comparers.Add(propertyType, comparer);
            }

            comparer.SetPropertyAndDirection(property, direction);
            itemsList.Sort(comparer);

            
this .propertyDescriptor  =  property;
            
this .listSortDirection  =  direction;
            
this .isSorted  =   true ;

            
this .OnListChanged( new  ListChangedEventArgs(ListChangedType.Reset,  - 1 ));
        }

        
protected   override   void  RemoveSortCore()
        {
            
this .isSorted  =   false ;
            
this .propertyDescriptor  =   base .SortPropertyCore;
            
this .listSortDirection  =   base .SortDirectionCore;

            
this .OnListChanged( new  ListChangedEventArgs(ListChangedType.Reset,  - 1 ));
        }

        
protected   override   int  FindCore(PropertyDescriptor property,  object  key)
        {
            
int  count  =   this .Count;
            
for  ( int  i  =   0 ; i  <  count;  ++ i)
            {
                T element 
=   this [i];
                
if  (property.GetValue(element).Equals(key))
                {
                    
return  i;
                }
            }

            
return   - 1 ;
        }
    }


ExpandedBlockStart.gif PropertyComparer
public   class  PropertyComparer < T >  : IComparer < T >
    {
        
private   readonly  IComparer comparer;
        
private  PropertyDescriptor propertyDescriptor;
        
private   int  reverse;

        
public  PropertyComparer(PropertyDescriptor property, ListSortDirection direction)
        {
            
this .propertyDescriptor  =  property;
            Type comparerForPropertyType 
=   typeof (Comparer <> ).MakeGenericType(property.PropertyType);
            
this .comparer  =  (IComparer)comparerForPropertyType.InvokeMember( " Default " , BindingFlags.Static  |  BindingFlags.GetProperty  |  BindingFlags.Public,  null null null );
            
this .SetListSortDirection(direction);
        }

        
#region  IComparer<T> Members

        
public   int  Compare(T x, T y)
        {
            
return   this .reverse  *   this .comparer.Compare( this .propertyDescriptor.GetValue(x),  this .propertyDescriptor.GetValue(y));
        }

        
#endregion

        
private   void  SetPropertyDescriptor(PropertyDescriptor descriptor)
        {
            
this .propertyDescriptor  =  descriptor;
        }

        
private   void  SetListSortDirection(ListSortDirection direction)
        {
            
this .reverse  =  direction  ==  ListSortDirection.Ascending  ?   1  :  - 1 ;
        }

        
public   void  SetPropertyAndDirection(PropertyDescriptor descriptor, ListSortDirection direction)
        {
            
this .SetPropertyDescriptor(descriptor);
            
this .SetListSortDirection(direction);
        }
    }


用法和简单 

List<T> test= new List<T>();

SortableBindingList<T> sb = new SortableBindingList<T>(test); 

posted on 2010-01-05 14:23 王玉升 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/OldWang/archive/2010/01/05/1639623.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值