winform datagridview binding does not support generic collections become sort of solution

Original: WinForm Binding the DataGridView become generic collection does not support the sort of solution

Case:

  Environment: Winform program

  Controls: Datagridview

  Phenomenon: Datagridview control is bound to a List <T> generic data does not support sorting

     Datagridview control is bound to a DataTable can support sorting

  Conclusion: Generics will lose ordering property Datagridview

  Solution: implement BindingList <T> interfaces

  Implementation code:

  

Copy the code
  . 1  the using the System;
   2  the using the System.Collections.Generic;
   . 3  the using the System.ComponentModel;
   . 4  the using the System.Linq;
   . 5  the using the System.Reflection;
   . 6  the using the System.Text;
   . 7  
  . 8  namespace HOET.Plugins.Orders.Model
   . 9  {
 10      / //  <the Summary> 
11      /// generic lose DataTable properties, DataGridView bind List <T> is not supported after ordering
 12      ///  </ the Summary> 
13      ///  <typeparam name = "T"> </ typeparam > 
14      class SortableBindingList<T> : BindingList<T>
 15     {
 16         private bool isSortedCore = true;
 17         private ListSortDirection sortDirectionCore = ListSortDirection.Ascending;
 18         private PropertyDescriptor sortPropertyCore = null;
 19         private string defaultSortItem;
 20 
 21         public SortableBindingList() : base() { }
 22 
 23         public SortableBindingList(IList<T> list) : base(list) { }
 24 
 25         protected  override  BOOL SupportsSortingCore
 26          {
 27              get { return  true ; }
 28          }
 29  
30          protected  override  BOOL SupportsSearchingCore
 31          {
 32              get { return  true ; }
 33          }
 34  
35          protected  override  BOOL IsSortedCore
 36          {
 37              get { return isSortedCore; }
 38         }
 39 
 40         protected override ListSortDirection SortDirectionCore
 41         {
 42             get { return sortDirectionCore; }
 43         }
 44 
 45         protected override PropertyDescriptor SortPropertyCore
 46         {
 47             get { return sortPropertyCore; }
 48         }
 49 
 50         protected override int FindCore(PropertyDescriptor prop, object key)
 51         {
 52             for(int i = 0; i < this.Count; i++)
 53             {
 54                 if(Equals(prop.GetValue(this[i]),key))
 55                 {
 56                     return i;
 57                 }
 58             }
 59 
 60             return -1;
 61         }
 62 
 63         protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
 64         {
65              isSortedCore = true ;
66              sortPropertyCore = prop;
67              sortDirectionCore = direction;
68              Sort ();
69          }
 70  
71          protected  override  void RemoveSortCore ()
 72          {
 73              ow (isSortedCore)
 74              {
 75                  isSortedCore = false ;
76                  sortPropertyCore = null ;
77                 sortDirectionCore = ListSortDirection.Ascending;
 78                 Sort();
 79             }
 80         }
 81 
 82         public string DefaultSortItem
 83         {
 84             get
 85             {
 86                 return defaultSortItem;
 87             }
 88             set
 89             {
 90                 if(defaultSortItem != value)
 91                 {
 92                     defaultSortItem = value;
 93                     Sort();
 94                 }
 95             }
 96         }
 97 
 98         private void Sort()
 99         {
100             List<T> list = this.Items as List<T>;
101             list.Sort(CompareCore);
102             ResetBindings();
103         }
104 
105         private int CompareCore(T o1, T o2)
106         {
107             int ret = 0;
108             if(SortPropertyCore != null)
109             {
110                 ret = CompareValue(SortPropertyCore.GetValue(o1), SortPropertyCore.GetValue(o2), SortPropertyCore.PropertyType);
111             }
112             if(ret == 0 && DefaultSortItem != null)
113             {
114                 PropertyInfo property = typeof(T).GetProperty(DefaultSortItem, BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.IgnoreCase, null, null, new Type[0], null);
115                 if(property != null)
116                 {
117                     ret = CompareValue(property.GetValue(o1, null), property.GetValue(o2, null), property.PropertyType);
118                 }
119             }
120             if(SortDirectionCore == ListSortDirection.Descending)
121             {
122                 ret = -ret;
123             }
124 
125             return ret;
126         }
127 
128         private static int CompareValue(object o1, object o2, Type type)
129         {
130             if(o1 == null)
131             {
132                 return o2 == null ? 0 : -1;
133             }
134             else if(o2 == null)
135             {
136                 return 1;
137             }
138             else if(type == typeof(char))
139             {
140                 return String.Compare(o1.ToString().Trim(), o2.ToString().Trim());
141             }
142             else if (type.IsEnum ||type.IsPrimitive)
143             {
144                 return Convert.ToDouble(o1).CompareTo(Convert.ToDouble(o2));
145             }
146             else if(type == typeof(DateTime))
147             {
148                 return Convert.ToDateTime(o1).CompareTo(o2);
149             }
150             else
151             {
152                 return String.Compare(o1.ToString().Trim(), o2.ToString().Trim());
153             }
154         }
155     }
156 }
Copy the code

 

  

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/11717409.html