counter
counter

根据反射排序泛型

摘抄部分:http://www.cnblogs.com/neru/archive/2011/01/06/1927519.html  

1.由于要用到点击Gredview某一列实现排序,Gridview是由泛型绑定的。如果是由Datatable绑定的,直接用Gridview的sort方法。                                                          GridView.Sort(expression,direction);

   解决办法:第一种:把list泛型转换为Datatable,用Gridview自带的sort方法;

      第二种:做一个通用的对list泛型某一属性排序的方法。由于list自带的排序满足不了动态的属性排序。

下面是第二种,

前台:

前台代码
 1 <asp:GridView ID="gvList" runat="server" AutoGenerateColumns="false"  
 2      GridLines="None" Width="100%" DataKeyNames="SchoolId" OnRowCommand="gvList_RowCommand"
 3     OnRowDataBound="gvList_RowDataBound" AllowSorting="true"  OnSorting="gvList_Sorting"  OnRowCreated="gvList_RowCreated">
 4                                 <Columns>
 5                                     <asp:TemplateField>
 6                                         <ItemTemplate>
 7                                             <%#Container.DataItemIndex + 1%>
 8                                         </ItemTemplate>
 9                                         <ItemStyle CssClass="x-grid4-td-numberer" />
10                                         <HeaderStyle CssClass="x-grid4-hd-cell" HorizontalAlign="Center" />
11                                     </asp:TemplateField>     
12                                     <asp:BoundField DataField="SchoolId" HeaderText="学校" SortExpression="Id">
13                                         <HeaderStyle CssClass="x-grid4-hd-cell" />
14                                         <ItemStyle CssClass="x-grid4-row" HorizontalAlign="center" />
15                                     </asp:BoundField>
16                   </Columns>
17  </asp:GridView>

 

后台:修改后,测试可用。

 

后台代码
  1   protected void gvList_Sorting(object sender, GridViewSortEventArgs e)
  2         {
  3             //设置需排序的属性。
  4             if (sortExpression != e.SortExpression)
  5             {
  6                 sortExpression = e.SortExpression;
  7             }
  8             else
  9             {
 10             }
 11             //设置排序的方式。
 12             if (sortDirection == SortDirection.Ascending)
 13             {
 14                 sortDirection = SortDirection.Descending;
 15             }
 16             else
 17             {
 18                 sortDirection = SortDirection.Ascending;
 19             }
 20             //刷新列表。
 21             this.Pagingbar.RefreshCurrentPage();
 22         }
 23 
 24         /// <summary>
 25         /// 为列表表头添加图片标识。
 26         /// </summary>
 27         /// <param name="sender"></param>
 28         /// <param name="e"></param>
 29         protected void gvList_RowCreated(object sender, GridViewRowEventArgs e)
 30         {
 31             if (e.Row.RowType == DataControlRowType.Header)
 32             {
 33                 int sortColumnIndex = GetSortColumnIndex();
 34 
 35                 if (sortColumnIndex != -1)
 36                 {
 37                     AddSortImage(sortColumnIndex, e.Row);
 38                 }
 39             }
 40         }
 41 
 42         /// <summary>
 43         ///列表表头添加图片标识。
 44         /// </summary>
 45         /// <param name="columnIndex">列号。</param>
 46         /// <param name="headerRow">列表行。</param>
 47         private void AddSortImage(int columnIndex, GridViewRow headerRow)
 48         {
 49             Image sortImage = new Image();
 50             if (sortDirection == SortDirection.Ascending)
 51             {
 52                 sortImage.ImageUrl = "~/images/img_top.gif";
 53             }
 54             else
 55             {
 56                 sortImage.ImageUrl = "~/images/page_attach.png";
 57             }
 58 
 59             headerRow.Cells[columnIndex].Controls.Add(sortImage);
 60         }
 61         
 62         /// <summary>
 63         /// 获得列表的列号。
 64         /// </summary>
 65         /// <returns>列表的列号。</returns>
 66         private int GetSortColumnIndex()
 67         {
 68             foreach (DataControlField field in gvList.Columns)
 69             {
 70                 if (field.SortExpression == sortExpression)
 71                 {
 72                     return gvList.Columns.IndexOf(field);
 73                 }
 74             }
 75             return -1;
 76         }
 77 
 78         /// <summary>
 79         /// 排序字段。
 80         /// </summary>
 81         private static string sortExpression=string.Empty;
 82 
 83         /// <summary>
 84         /// 排序顺序(升序或者降序)。
 85         /// </summary>
 86         private static SortDirection sortDirection;
 87        
 88         #region Fields...
 89 
 90         /// <summary>
 91         /// 初始化行号索引值。
 92         /// </summary>
 93         protected int _rowIndex = 0;   
 94 
 95     }
 96 
 97     /// <summary>
 98     ///排序类。
 99     /// </summary>
100     public class SortClass
101     {
102         private string _sortProperty;
103         private SortDirection _sortDirection;
104 
105         public string SortProperty
106         {
107             get { return _sortProperty; }
108             set { _sortProperty = value; }
109         }
110 
111         public SortDirection SortDirection
112         {
113             get { return _sortDirection; }
114             set { _sortDirection = value; }
115         }
116 
117         public SortClass(string sortProperty, SortDirection sortDirection)
118         {
119             _sortProperty = sortProperty;
120             _sortDirection = sortDirection;
121         }
122     }
123 
124 
125     /// <summary>
126     /// 实现排序接口类。
127     /// </summary>
128     public class Comparer<T> : IComparer<T>
129     {
130         private List<SortClass> _sortClasses;
131 
132         public List<SortClass> SortClasses
133         {
134             get { return _sortClasses; }
135         }
136 
137         public Comparer()
138         {
139             _sortClasses = new List<SortClass>();
140         }
141 
142         public Comparer(List<SortClass> sortClass)
143         {
144             _sortClasses = sortClass;
145         }
146 
147         public Comparer(string sortProperty, SortDirection sortDirection)
148         {
149             _sortClasses = new List<SortClass>();
150             _sortClasses.Add(new SortClass(sortProperty, sortDirection));
151         }
152 
153         public int Compare(T x, T y)
154         {
155             if (SortClasses.Count == 0)
156             {
157                 return 0;
158             }
159             return CheckSort(0, x, y);
160         }
161 
162         private int CheckSort(int sortLevel, T myObject1, T myObject2)
163         {
164             int returnVal = 0;
165             if (SortClasses.Count - 1 >= sortLevel)
166             {
167                 object valueOf1 = myObject1.GetType().GetProperty(SortClasses[sortLevel].SortProperty).GetValue(myObject1, null);
168                 object valueOf2 = myObject2.GetType().GetProperty(SortClasses[sortLevel].SortProperty).GetValue(myObject2, null);
169                 if (SortClasses[sortLevel].SortDirection == SortDirection.Ascending)
170                 {
171                     returnVal = ((IComparable)valueOf1).CompareTo((IComparable)valueOf2);
172                 }
173                 else
174                 {
175                     returnVal = ((IComparable)valueOf2).CompareTo((IComparable)valueOf1);
176                 }
177 
178                 if (returnVal == 0)
179                 {
180                     returnVal = CheckSort(sortLevel + 1, myObject1, myObject2);
181                 }
182             }
183             return returnVal;
184         }
185     }
186 
187     /// <summary>
188     /// 排序方法类。
189     /// </summary>
190     public class ListSorter
191     {
192         //对多个属性进行排序。
193         public static List<T> SortList<T>(List<T> listToSort, List<string> sortExpression, List<SortDirection> sortDirection)
194         {
195             if (sortExpression.Count != sortDirection.Count || sortExpression.Count == 0 || sortDirection.Count == 0)
196             {
197                 throw new Exception("无效的排序属性。");
198             }
199             Comparer<T> myComparer = new Comparer<T>();
200             for (int i = 0; i < sortExpression.Count; i++)
201             {
202                 SortClass sortClass = new SortClass(sortExpression[i], sortDirection[i]);
203                 myComparer.SortClasses.Add(sortClass);
204             }
205 
206             listToSort.Sort(myComparer);
207             return listToSort;
208         }
209 
210         //对单个属性进行排序。
211         public static List<T> SortList<T>(List<T> listToSort, string sortExpression, SortDirection sortDirection)
212         {
213             if (sortExpression == null || sortExpression == string.Empty)
214             {
215                 return listToSort;
216             }
217             Comparer<T> myComparer = new Comparer<T>();
218             myComparer.SortClasses.Add(new SortClass(sortExpression, sortDirection));
219             listToSort.Sort(myComparer);
220             return listToSort;
221         }
222     }

 

使用:
        //使用ListSorter类的静态方法,对listInfo泛型集合的单个属性进行排序。
List<ServiceInfo> sortedServiceInfo = ListSorter.SortList(listInfo, sortExpression, sortDirection);

 





 
posted @ 2012-08-13 10:24  bfy  阅读(263)  评论(0编辑  收藏  举报