GridView绑定自定义数据源(list)排序问题

问题:将list数据源绑定到GridView后,排序没有效果。e.SortDirection始终是SortDirection.Ascending。
将list数据源以Gridview1.DataSource的方式绑定到GridView1 ContractedBlock.gifCode
default2.aspx的代码

ContractedBlock.gif ExpandedBlockStart.gif Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<%@ Register Assembly="myWebUserControl" Namespace="myWebUserControl" TagPrefix="cc1" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>无标题页</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
&nbsp;<asp:GridView ID="GridView1" runat="server">
            
<Columns>
                
<asp:BoundField DataField="FileName" SortExpression="FileName" />
            
</Columns>
            
<PagerSettings 
             Mode
="NextPreviousFirstLast"
             
/>
        
</asp:GridView>
       
</div>
    
</form>
</body>
</html>


default2.aspx.cs的代码

ContractedBlock.gif ExpandedBlockStart.gif Code
  1  using  System;
  2  using  System.Data;
  3  using  System.Configuration;
  4  using  System.Collections;
  5  using  System.Web;
  6  using  System.Web.Security;
  7  using  System.Web.UI;
  8  using  System.Web.UI.WebControls;
  9  using  System.Web.UI.WebControls.WebParts;
 10  using  System.Web.UI.HtmlControls;
 11  using  System.Web.UI.MobileControls;
 12  using  System.Collections.Generic;
 13  using  myWebUserControl;
 14  using  System.Reflection;
 15 
 16 
 17  [Serializable]
 18  public   class  mySize
 19  {
 20       private   int  _Size  =   0 ;
 21 
 22       public   int  Size
 23      {
 24           get  {  return  _Size; }
 25           set  { _Size  =  value; }
 26      }
 27 
 28 
 29       public  mySize( int  size)
 30      {
 31          Size = size;
 32      }
 33  }
 34 
 35  [Serializable]
 36  public   class  myFile
 37  {
 38       private  DateTime _FileName ;
 39 
 40       public  DateTime FileName
 41      {
 42           get  {  return  _FileName; }
 43           set  { _FileName  =  value; }
 44      }
 45       private  mySize _mySize  =   null ;
 46 
 47       public  mySize mySize
 48      {
 49           get  {  return  _mySize; }
 50           set  { _mySize  =  value; }
 51      }
 52       public  myFile(DateTime filename, int  size)
 53      {
 54          FileName  =  filename;
 55          mySize  =   new  mySize(size);
 56      }
 57  }
 58 
 59  public   partial   class  Default2 : System.Web.UI.Page
 60  {
 61 
 62      DateTime date  =  DateTime.Now;
 63      BindListGridViewSortUtil bindListUtil  =   null ;
 64       protected   void  Page_Load( object  sender, EventArgs e)
 65      {
 66 
 67           if  ( ! IsPostBack || ViewState[ " bindUtil " ] == null )
 68          {
 69              
 70              List < myFile >  filelist  =   new  List < myFile > ();
 71               for  ( int  i  =   0 ; i  <   250 ; i ++ )
 72              {
 73                  date  =  date.AddDays( 1 );
 74                  filelist.Add( new  myFile(date, i));
 75              }
 76 
 77              GridView1.AllowPaging  =   true ;
 78              GridView1.AllowSorting  =   true ;
 79 
 80              bindListUtil  =   new  BindListGridViewSortUtil();
 81              bindListUtil.Result  =  filelist;
 82              ViewState[ " bindUtil " =  bindListUtil;    
 83          }
 84 
 85 
 86           if  (ViewState[ " bindUtil " !=   null )
 87          {
 88              bindListUtil  =  (BindListGridViewSortUtil)ViewState[ " bindUtil " ];
 89          }
 90          bindListUtil.BindListToGridView < myFile > (GridView1,  null );
 91          GridView1.DataBind();
 92      }
 93 
 94  }
 95 
 96  [Serializable]
 97  public   class  BindListGridViewSortUtil
 98  {
 99 
100       #region  Filed
101       private   object  _Result  =   new   object ();
102 
103       ///   <summary>
104       ///  排序后的结果集
105       ///   </summary>
106       public   object  Result
107      {
108           get  {  return  _Result; }
109           set  { _Result  =  value; }
110      }
111 
112       private   bool  _AutoFill  =   true ;
113 
114       ///   <summary>
115       ///  自动填充模式,即如果内部结果集Result有值,则优先使用内部结果集进行填充。
116       ///   </summary>
117       public   bool  AutoFill
118      {
119           get  {  return  _AutoFill; }
120           set  { _AutoFill  =  value; }
121      }
122 
123       private   string  _SortExpression  =   "" ;
124 
125       ///   <summary>
126       ///  要进行排序的!属性!名称
127       ///   </summary>
128       public   string  SortExpression
129      {
130           get  {  return  _SortExpression; }
131      }
132 
133       private   System.Web.UI.WebControls.SortDirection _mySortDirection  =  System.Web.UI.WebControls.SortDirection.Ascending;
134      
135       ///   <summary>
136       ///  GridView的排序方式,递增,递减
137       ///   </summary>
138       public   System.Web.UI.WebControls.SortDirection mySortDirection
139      {
140           get  {  return  _mySortDirection; }
141           // set { _mySortDirection = value; }
142      }
143 
144       #endregion
145 
146 
147       #region  Methods
148       ///   <summary>
149       ///  GridView的分页响应事件
150       ///   </summary>
151       ///   <param name="sender"></param>
152       ///   <param name="e"></param>
153       public   void  BindListGridView1_PageIndexChanging( object  sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
154      {
155          System.Web.UI.WebControls.GridView bindListGridView  =  sender  as  System.Web.UI.WebControls.GridView;
156           if  (sender  ==   null )
157          {
158               return ;
159          }
160          bindListGridView.PageIndex  =  e.NewPageIndex;
161          bindListGridView.DataBind();
162      }
163 
164       ///   <summary>
165       ///  GridView的排序响应时间
166       ///   </summary>
167       ///   <typeparam name="T"></typeparam>
168       ///   <param name="sender"></param>
169       ///   <param name="e"></param>
170       public   void  BindListGridView1_Sorting < T > ( object  sender, System.Web.UI.WebControls.GridViewSortEventArgs e)
171      {
172           System.Web.UI.WebControls.GridView bindListGridView  =  sender  as  System.Web.UI.WebControls.GridView;
173           if  (sender  ==   null )
174          {
175               return ;
176          }
177 
178          _SortExpression  =  e.SortExpression;
179          List < T >  sortResult = new  List < T > ();
180 
181          sortResult  =  ListSort < T > (bindListGridView.DataSource  as  List < T > );                             // 对DataSource进行升序排序
182 
183 
184           if  (mySortDirection  ==  System.Web.UI.WebControls.SortDirection.Ascending)             
185          {
186              _mySortDirection  =  System.Web.UI.WebControls.SortDirection.Descending;
187          }
188           else
189          {
190              sortResult.Reverse();                                                                    // 如果需要降序排序,则将结果倒转一次。
191              _mySortDirection  =  System.Web.UI.WebControls.SortDirection.Ascending;
192          }
193 
194          Result  =  sortResult;
195          bindListGridView.DataSource  =  sortResult;
196          bindListGridView.DataBind();
197      }
198 
199 
200       ///   <summary>
201       ///  对List按照特定属性进行排序
202       ///   </summary>
203       ///   <typeparam name="T"> List <T> 中存储的变量类型 </typeparam>
204       ///   <param name="list"></param>
205       ///   <returns></returns>
206       public  System.Collections.Generic.List < T >  ListSort < T > (System.Collections.Generic.List < T >  list)
207      {
208           if  (list  ==   null )
209          {
210               return   null ;
211          }
212 
213          list.Sort( new  Comparison < T > (CompareBigThan < T > ));
214           return  list;
215      }
216 
217       ///   <summary>
218       ///  对两个特定类型的变量按照其中某个属性的大小进行排序
219       ///   </summary>
220       ///   <typeparam name="T"> 该变量的类型 </typeparam>
221       ///   <param name="objA"> 比较值一 </param>
222       ///   <param name="objB"> 比较值二 </param>
223       ///   <returns> 结果 </returns>
224       public   int  myCompare < T > (T objA, T objB)
225      {
226           if  (objA  ==   null   ||  objB  ==   null )
227          {
228               return   0 ;                                // 任意一个比较值为null就返回0值。
229          }                                                   
230 
231          System.Type t  =  objA.GetType();
232          System.Reflection.PropertyInfo p  =  t.GetProperty(SortExpression);
233 
234           if  (p  !=   null )
235          {
236               object  objA_Comparevalue  =  p.GetValue(objA,  null );
237               object  objB_Comparevalue  =  p.GetValue(objB,  null );
238 
239               //
240               // 判断该属性的类型,并按该类型的比较方式进行比较
241               //
242               //
243 
244               if  (objA_Comparevalue.GetType()  ==   typeof (DateTime))
245              {
246                   return  DateTime.Compare((DateTime)objA_Comparevalue, (DateTime)objB_Comparevalue);
247              }
248               if  (objA_Comparevalue.GetType()  ==   typeof ( int ))
249              {
250                   return  ( int )objA_Comparevalue  -  ( int )objB_Comparevalue;
251              }
252               if  (objA_Comparevalue.GetType()  ==   typeof ( string ))
253              {
254                   return   string .Compare(objA_Comparevalue.ToString(), objB_Comparevalue.ToString());
255              }
256               if  (objA_Comparevalue.GetType()  ==   typeof ( long ))
257              {
258                   return  ( int )(( long )objA_Comparevalue  -  ( long )objB_Comparevalue);
259              }
260               if  (objA_Comparevalue.GetType()  ==   typeof ( double ))
261              {
262                   return  ( int )(( double )objA_Comparevalue  -  ( double )objB_Comparevalue);
263              }
264 
265 
266               return   0 ;
267 
268          }
269 
270           return   0 ;
271           //
272           // 比较值的类型中没有该属性,以及不在能进行比较范围内的类型返回值为0。
273           //
274      }
275 
276       public   int  CompareBigThan < T > (T objA, T objB)
277      {
278           return  myCompare < T > (objB, objA);
279      }
280 
281       ///   <summary>
282       ///  将数据源绑定到GridView
283       ///   </summary>
284       ///   <typeparam name="T"> 数据源对象的类型 </typeparam>
285       ///   <param name="gridViewToBind"> GridView对象 </param>
286       ///   <param name="dataSource"> 绑定到GridView的数据源。当AutoFile为false时或Result结果集无值时使用该值。 </param>
287       public   void  BindListToGridView < T > (System.Web.UI.WebControls.GridView gridViewToBind, List < T >  dataSource)
288      {
289          gridViewToBind.PageIndexChanging  +=   new  GridViewPageEventHandler( this .BindListGridView1_PageIndexChanging);
290          gridViewToBind.Sorting  +=   new  GridViewSortEventHandler( this .BindListGridView1_Sorting < T > );
291 
292 
293          List < T >  data  =   this .Result  as  List < T > ;
294 
295           if  (AutoFill && data != null )
296          {
297              gridViewToBind.DataSource  =  (List < T > ) this .Result;
298          }
299           else
300          {
301              gridViewToBind.DataSource = dataSource;
302          }
303      }
304 
305       #endregion
306  }

转载于:https://www.cnblogs.com/klvk/archive/2009/08/05/1539359.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值