![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeBehind
=
"
Default.aspx.cs
"
Inherits
=
"
GridViewSorting._Default
"
%>
2
3 <! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
4
5 < html xmlns = " http://www.w3.org/1999/xhtml " >
6 < head runat = " server " >
7 < meta http - equiv = " Content-Type " content = " text/html; charset=utf-8 " />
8 < title > Csharp GridView Sorting 字段排序用法 </ title >
9 </ head >
10 < body >
11 < form id = " form1 " runat = " server " >
12 < div >
13 < asp:GridView ID = " GridView1 " runat = " server " AutoGenerateColumns = " False " OnSorting = " GridView1_Sorting " AllowPaging = " True " AllowSorting = " True " >
14 < Columns >
15 < asp:TemplateField HeaderText = " Id " SortExpression = " ApplicationID " >
16 < ItemTemplate >
17 < asp:Label ID = " lblbigid " Text = ' <%# Eval("ApplicationID") %> ' runat = " server " ></ asp:Label >
18 </ ItemTemplate >
19 < EditItemTemplate >
20 < asp:Label ID = " lblbigid " Text = ' <%# Eval("ApplicationID") %> ' runat = " server " ></ asp:Label >
21 </ EditItemTemplate >
22 < FooterTemplate >
23 </ FooterTemplate ></ asp:TemplateField >
24 < asp:TemplateField HeaderText = " 姓名 " SortExpression = " ApplicationName " >
25 < ItemTemplate >
26 < asp:Label ID = " lblbigname " Text = ' <%# Eval("ApplicationName") %> ' runat = " server " ></ asp:Label >
27 </ ItemTemplate >
28 < EditItemTemplate >
29 < asp:TextBox ID = " txtbigname " Text = ' <%# Eval("ApplicationName") %> ' runat = " server " ></ asp:TextBox >
30 </ EditItemTemplate >
31 < FooterTemplate >
32 < asp:TextBox ID = " txtbigname " Text = '' runat = " server " ></ asp:TextBox >
33 </ FooterTemplate >
34 </ asp:TemplateField >
35 < asp:TemplateField HeaderText = " 职称 " SortExpression = " ServerName " >
36 < ItemTemplate >
37 < asp:Label ID = " lblServerName " Text = ' <%# Eval("ServerName") %> ' runat = " server " ></ asp:Label >
38 </ ItemTemplate >
39 < EditItemTemplate >
40 < asp:TextBox ID = " txtServerName " Text = ' <%# Eval("ServerName") %> ' runat = " server " ></ asp:TextBox >
41 </ EditItemTemplate >
42 < FooterTemplate >
43 < asp:TextBox ID = " txtServerName " Text = '' runat = " server " ></ asp:TextBox >
44 </ FooterTemplate >
45 </ asp:TemplateField >
46 </ Columns >
47 </ asp:GridView >
48
49 </ div >
50 </ form >
51 </ body >
52 </ html >
53
2
3 <! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
4
5 < html xmlns = " http://www.w3.org/1999/xhtml " >
6 < head runat = " server " >
7 < meta http - equiv = " Content-Type " content = " text/html; charset=utf-8 " />
8 < title > Csharp GridView Sorting 字段排序用法 </ title >
9 </ head >
10 < body >
11 < form id = " form1 " runat = " server " >
12 < div >
13 < asp:GridView ID = " GridView1 " runat = " server " AutoGenerateColumns = " False " OnSorting = " GridView1_Sorting " AllowPaging = " True " AllowSorting = " True " >
14 < Columns >
15 < asp:TemplateField HeaderText = " Id " SortExpression = " ApplicationID " >
16 < ItemTemplate >
17 < asp:Label ID = " lblbigid " Text = ' <%# Eval("ApplicationID") %> ' runat = " server " ></ asp:Label >
18 </ ItemTemplate >
19 < EditItemTemplate >
20 < asp:Label ID = " lblbigid " Text = ' <%# Eval("ApplicationID") %> ' runat = " server " ></ asp:Label >
21 </ EditItemTemplate >
22 < FooterTemplate >
23 </ FooterTemplate ></ asp:TemplateField >
24 < asp:TemplateField HeaderText = " 姓名 " SortExpression = " ApplicationName " >
25 < ItemTemplate >
26 < asp:Label ID = " lblbigname " Text = ' <%# Eval("ApplicationName") %> ' runat = " server " ></ asp:Label >
27 </ ItemTemplate >
28 < EditItemTemplate >
29 < asp:TextBox ID = " txtbigname " Text = ' <%# Eval("ApplicationName") %> ' runat = " server " ></ asp:TextBox >
30 </ EditItemTemplate >
31 < FooterTemplate >
32 < asp:TextBox ID = " txtbigname " Text = '' runat = " server " ></ asp:TextBox >
33 </ FooterTemplate >
34 </ asp:TemplateField >
35 < asp:TemplateField HeaderText = " 职称 " SortExpression = " ServerName " >
36 < ItemTemplate >
37 < asp:Label ID = " lblServerName " Text = ' <%# Eval("ServerName") %> ' runat = " server " ></ asp:Label >
38 </ ItemTemplate >
39 < EditItemTemplate >
40 < asp:TextBox ID = " txtServerName " Text = ' <%# Eval("ServerName") %> ' runat = " server " ></ asp:TextBox >
41 </ EditItemTemplate >
42 < FooterTemplate >
43 < asp:TextBox ID = " txtServerName " Text = '' runat = " server " ></ asp:TextBox >
44 </ FooterTemplate >
45 </ asp:TemplateField >
46 </ Columns >
47 </ asp:GridView >
48
49 </ div >
50 </ form >
51 </ body >
52 </ html >
53
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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
12 namespace GridViewSorting
13 {
14 /// <summary>
15 /// 2010-09-16 字段排序用法
16 /// Geovin Du 涂聚文
17 /// </summary>
18 public partial class _Default : System.Web.UI.Page
19 {
20 protected void Page_Load( object sender, EventArgs e)
21 {
22 if ( ! IsPostBack)
23 {
24 // 首次回载页面排序
25 ViewState[ " sortExpression " ] = " ApplicationID " ;
26 ViewState[ " sort " ] = " DESC " ;
27 GridBind();
28 // Binding GridView with the datasource
29 // this.GridView1.DataSource = FillDataTable();
30 // this.GridView1.DataBind();
31 }
32 }
33 /// <summary>
34 ///
35 /// </summary>
36 /// <param name="sender"></param>
37 /// <param name="e"></param>
38 protected void GridView1_Sorting( object sender, GridViewSortEventArgs e)
39 {
40
41 ViewState[ " sortExpression " ] = e.SortExpression;
42 if (ViewState[ " sort " ].ToString() == " DESC " )
43 {
44 ViewState[ " sort " ] = " ASC " ;
45 GridBind();
46 }
47 else
48 {
49 ViewState[ " sort " ] = " DESC " ;
50 GridBind();
51 }
52
53 }
54 /// <summary>
55 /// Method that will add few rows in a DataTable and returns a DataTable
56 /// </summary>
57 /// <returns> DataTable </returns>
58 protected DataTable FillDataTable()
59 {
60 DataTable dt = new DataTable();
61 dt.Columns.Add( " ApplicationID " );
62 dt.Columns.Add( " ApplicationName " );
63 dt.Columns.Add( " ServerName " );
64
65 DataRow dr = dt.NewRow();
66 dr[ 0 ] = " 1 " ;
67 dr[ 1 ] = " 涂斯博 " ;
68 dr[ 2 ] = " 硬件工程师 " ;
69 dt.Rows.Add(dr);
70
71 DataRow dr1 = dt.NewRow();
72 dr1[ 0 ] = " 2 " ;
73 dr1[ 1 ] = " 杜甫 " ;
74 dr1[ 2 ] = " 美学工程师 " ;
75 dt.Rows.Add(dr1);
76
77 DataRow dr2 = dt.NewRow();
78 dr2[ 0 ] = " 3 " ;
79 dr2[ 1 ] = " 刘杰 " ;
80 dr2[ 2 ] = " 网络工程师 " ;
81 dt.Rows.Add(dr2);
82
83 DataRow dr3 = dt.NewRow();
84 dr3[ 0 ] = " 4 " ;
85 dr3[ 1 ] = " 涂聚文 " ;
86 dr3[ 2 ] = " 软件工程师 " ;
87 dt.Rows.Add(dr3);
88
89 DataRow dr4 = dt.NewRow();
90 dr4[ 0 ] = " 5 " ;
91 dr4[ 1 ] = " 赵金红 " ;
92 dr4[ 2 ] = " 公共管理师 " ;
93 dt.Rows.Add(dr4);
94
95 DataRow dr5 = dt.NewRow();
96 dr5[ 0 ] = " 6 " ;
97 dr5[ 1 ] = " 江明 " ;
98 dr5[ 2 ] = " 架构工程师 " ;
99 dt.Rows.Add(dr5);
100
101 DataRow dr6 = dt.NewRow();
102 dr5[ 0 ] = " 7 " ;
103 dr5[ 1 ] = " 达成 " ;
104 dr5[ 2 ] = " 数据库工程师 " ;
105 dt.Rows.Add(dr6);
106
107 return dt;
108 }
109
110 /// <summary>
111 /// 排序绑定
112 /// </summary>
113 private void GridBind()
114 {
115 // DataSet 用法
116 // DataSet myset = FillDataTable();
117 DataView myview = FillDataTable().DefaultView; // DataSet用法: myset.Tables[0].DefaultView;
118 myview.Sort = ViewState[ " sortExpression " ].ToString() + ViewState[ " sort " ].ToString();
119 GridView1.DataSource = myview;
120 GridView1.DataBind();
121 }
122 }
123 }
124
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
12 namespace GridViewSorting
13 {
14 /// <summary>
15 /// 2010-09-16 字段排序用法
16 /// Geovin Du 涂聚文
17 /// </summary>
18 public partial class _Default : System.Web.UI.Page
19 {
20 protected void Page_Load( object sender, EventArgs e)
21 {
22 if ( ! IsPostBack)
23 {
24 // 首次回载页面排序
25 ViewState[ " sortExpression " ] = " ApplicationID " ;
26 ViewState[ " sort " ] = " DESC " ;
27 GridBind();
28 // Binding GridView with the datasource
29 // this.GridView1.DataSource = FillDataTable();
30 // this.GridView1.DataBind();
31 }
32 }
33 /// <summary>
34 ///
35 /// </summary>
36 /// <param name="sender"></param>
37 /// <param name="e"></param>
38 protected void GridView1_Sorting( object sender, GridViewSortEventArgs e)
39 {
40
41 ViewState[ " sortExpression " ] = e.SortExpression;
42 if (ViewState[ " sort " ].ToString() == " DESC " )
43 {
44 ViewState[ " sort " ] = " ASC " ;
45 GridBind();
46 }
47 else
48 {
49 ViewState[ " sort " ] = " DESC " ;
50 GridBind();
51 }
52
53 }
54 /// <summary>
55 /// Method that will add few rows in a DataTable and returns a DataTable
56 /// </summary>
57 /// <returns> DataTable </returns>
58 protected DataTable FillDataTable()
59 {
60 DataTable dt = new DataTable();
61 dt.Columns.Add( " ApplicationID " );
62 dt.Columns.Add( " ApplicationName " );
63 dt.Columns.Add( " ServerName " );
64
65 DataRow dr = dt.NewRow();
66 dr[ 0 ] = " 1 " ;
67 dr[ 1 ] = " 涂斯博 " ;
68 dr[ 2 ] = " 硬件工程师 " ;
69 dt.Rows.Add(dr);
70
71 DataRow dr1 = dt.NewRow();
72 dr1[ 0 ] = " 2 " ;
73 dr1[ 1 ] = " 杜甫 " ;
74 dr1[ 2 ] = " 美学工程师 " ;
75 dt.Rows.Add(dr1);
76
77 DataRow dr2 = dt.NewRow();
78 dr2[ 0 ] = " 3 " ;
79 dr2[ 1 ] = " 刘杰 " ;
80 dr2[ 2 ] = " 网络工程师 " ;
81 dt.Rows.Add(dr2);
82
83 DataRow dr3 = dt.NewRow();
84 dr3[ 0 ] = " 4 " ;
85 dr3[ 1 ] = " 涂聚文 " ;
86 dr3[ 2 ] = " 软件工程师 " ;
87 dt.Rows.Add(dr3);
88
89 DataRow dr4 = dt.NewRow();
90 dr4[ 0 ] = " 5 " ;
91 dr4[ 1 ] = " 赵金红 " ;
92 dr4[ 2 ] = " 公共管理师 " ;
93 dt.Rows.Add(dr4);
94
95 DataRow dr5 = dt.NewRow();
96 dr5[ 0 ] = " 6 " ;
97 dr5[ 1 ] = " 江明 " ;
98 dr5[ 2 ] = " 架构工程师 " ;
99 dt.Rows.Add(dr5);
100
101 DataRow dr6 = dt.NewRow();
102 dr5[ 0 ] = " 7 " ;
103 dr5[ 1 ] = " 达成 " ;
104 dr5[ 2 ] = " 数据库工程师 " ;
105 dt.Rows.Add(dr6);
106
107 return dt;
108 }
109
110 /// <summary>
111 /// 排序绑定
112 /// </summary>
113 private void GridBind()
114 {
115 // DataSet 用法
116 // DataSet myset = FillDataTable();
117 DataView myview = FillDataTable().DefaultView; // DataSet用法: myset.Tables[0].DefaultView;
118 myview.Sort = ViewState[ " sortExpression " ].ToString() + ViewState[ " sort " ].ToString();
119 GridView1.DataSource = myview;
120 GridView1.DataBind();
121 }
122 }
123 }
124