将list数据源以Gridview1.DataSource的方式绑定到GridView1
Code
default2.aspx的代码
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>
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:BoundField DataField="FileName" SortExpression="FileName" />
Columns>
<PagerSettings
Mode="NextPreviousFirstLast"
/>
asp:GridView>
div>
form>
body>
html>
<%@ 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>
<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的代码
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 ///
104 /// 排序后的结果集
105 ///
106 public object Result
107 {
108 get { return _Result; }
109 set { _Result = value; }
110 }
111
112 private bool _AutoFill = true ;
113
114 ///
115 /// 自动填充模式,即如果内部结果集Result有值,则优先使用内部结果集进行填充。
116 ///
117 public bool AutoFill
118 {
119 get { return _AutoFill; }
120 set { _AutoFill = value; }
121 }
122
123 private string _SortExpression = "" ;
124
125 ///
126 /// 要进行排序的!属性!名称
127 ///
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 ///
136 /// GridView的排序方式,递增,递减
137 ///
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 ///
149 /// GridView的分页响应事件
150 ///
151 ///
152 ///
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 ///
165 /// GridView的排序响应时间
166 ///
167 ///
168 ///
169 ///
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 ///
201 /// 对List按照特定属性进行排序
202 ///
203 ///
List
中存储的变量类型
204 ///
205 ///
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 ///
218 /// 对两个特定类型的变量按照其中某个属性的大小进行排序
219 ///
220 ///
该变量的类型
221 /// 比较值一
222 /// 比较值二
223 ///
结果
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 ///
282 /// 将数据源绑定到GridView
283 ///
284 ///
数据源对象的类型
285 /// GridView对象
286 /// 绑定到GridView的数据源。当AutoFile为false时或Result结果集无值时使用该值。
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 }
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 ///
104 /// 排序后的结果集
105 ///
106 public object Result
107 {
108 get { return _Result; }
109 set { _Result = value; }
110 }
111
112 private bool _AutoFill = true ;
113
114 ///
115 /// 自动填充模式,即如果内部结果集Result有值,则优先使用内部结果集进行填充。
116 ///
117 public bool AutoFill
118 {
119 get { return _AutoFill; }
120 set { _AutoFill = value; }
121 }
122
123 private string _SortExpression = "" ;
124
125 ///
126 /// 要进行排序的!属性!名称
127 ///
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 ///
136 /// GridView的排序方式,递增,递减
137 ///
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 ///
149 /// GridView的分页响应事件
150 ///
151 ///
152 ///
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 ///
165 /// GridView的排序响应时间
166 ///
167 ///
168 ///
169 ///
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 ///
201 /// 对List按照特定属性进行排序
202 ///
203 ///
204 ///
205 ///
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 ///
218 /// 对两个特定类型的变量按照其中某个属性的大小进行排序
219 ///
220 ///
221 /// 比较值一
222 /// 比较值二
223 ///
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 ///
282 /// 将数据源绑定到GridView
283 ///
284 ///
285 /// GridView对象
286 /// 绑定到GridView的数据源。当AutoFile为false时或Result结果集无值时使用该值。
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 }