62,046
社区成员
发帖
与我相关
我的任务
分享
1 using System;
2 using System.Data;
3 using System.Data.SqlClient;
4 using System.Configuration;
5 using System.Collections;
6 using System.Web;
7 using System.Web.Security;
8 using System.Web.UI;
9 using System.Web.UI.WebControls;
10 using System.Web.UI.WebControls.WebParts;
11 using System.Web.UI.HtmlControls;
12
13 public partial class Demo6 : System.Web.UI.Page
14 {
15 protected void Page_Load(object sender, EventArgs e)
16 {
17 if (Page.IsPostBack == false)
18 {
19 this.GridView.Attributes.Add("SortExpression", "UserID");
20 this.GridView.Attributes.Add("SortDirection", "ASC");
21
22 BindData();
23 }
24 }
25
26 public void BindData()
27 {
28 string sortExpression = this.GridView.Attributes["SortExpression"];
29 string sortDirection = this.GridView.Attributes["SortDirection"];
30
31 string strSql = "select UserID,C_Name,E_Name,QQ from Demo_User ";
32 DataTable dt = SqlHelper.ExecuteDataset(SqlHelper.CONN_STRING, CommandType.Text, strSql, null).Tables[0];
33
34 // 根据GridView排序数据列及排序方向设置显示的默认数据视图
35 if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
36 {
37 dt.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
38 }
39
40 GridView.DataSource = dt;
41 GridView.DataBind();
42 }
43
44 protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
45 {
46 GridView.PageIndex = e.NewPageIndex;
47 BindData();
48 }
49
50 protected void GridView_Sorting(object sender, GridViewSortEventArgs e)
51 {
52 // 从事件参数获取排序数据列
53 string sortExpression = e.SortExpression.ToString();
54 // 假定为排序方向为“顺序”
55 string sortDirection = "ASC";
56 // “ASC”与事件参数获取到的排序方向进行比较,进行GridView排序方向参数的修改
57 if (sortExpression == this.GridView.Attributes["SortExpression"])
58 {
59 //获得下一次的排序状态
60 sortDirection = (this.GridView.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
61 }
62 // 重新设定GridView排序数据列及排序方向
63 this.GridView.Attributes["SortExpression"] = sortExpression;
64 this.GridView.Attributes["SortDirection"] = sortDirection;
65 BindData();
66
67 }
68 }
1 using System;
2 using System.Data;
3 using System.Data.SqlClient;
4 using System.Configuration;
5 using System.Collections;
6 using System.Web;
7 using System.Web.Security;
8 using System.Web.UI;
9 using System.Web.UI.WebControls;
10 using System.Web.UI.WebControls.WebParts;
11 using System.Web.UI.HtmlControls;
12
13 public partial class Demo7 : System.Web.UI.Page
14 {
15 protected void Page_Load(object sender, EventArgs e)
16 {
17 if (Page.IsPostBack == false)
18 {
19 ViewState["SortOrder"] = "UserID";
20 ViewState["OrderDire"] = "ASC";
21
22 BindData();
23 }
24 }
25
26 public void BindData()
27 {
28 string sortExpression = this.GridView.Attributes["SortExpression"];
29 string sortDirection = this.GridView.Attributes["SortDirection"];
30
31 string strSql = "select UserID,C_Name,E_Name,QQ from Demo_User ";
32 DataTable dt = SqlHelper.ExecuteDataset(SqlHelper.CONN_STRING, CommandType.Text, strSql, null).Tables[0];
33
34 DataView dv = dt.DefaultView;
35 string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
36 dv.Sort = sort;
37
38 GridView.DataSource = dt;
39 GridView.DataBind();
40 }
41
42 protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
43 {
44 GridView.PageIndex = e.NewPageIndex;
45 BindData();
46 }
47
48 protected void GridView_Sorting(object sender, GridViewSortEventArgs e)
49 {
50 string sPage = e.SortExpression;
51 if (ViewState["SortOrder"].ToString() == sPage)
52 {
53 if (ViewState["OrderDire"].ToString() == "Desc")
54 ViewState["OrderDire"] = "ASC";
55 else
56 ViewState["OrderDire"] = "Desc";
57 }
58 else
59 {
60 ViewState["SortOrder"] = e.SortExpression;
61 }
62 BindData();
63
64 }
65 }
66
67
68
1 <table align="center" bgcolor="#c0de98" border="0" cellpadding="0" cellspacing="1" width="99%">
2 <tr>
3 <th colspan="2">
4 GridView演示</th>
5 </tr>
6 <tr>
7 <td colspan="2" style="width: 100%;" >
8 <asp:GridView ID="GridView" runat="server" Width="100%" AutoGenerateColumns="False" AllowPaging="True" OnPageIndexChanging="GridView_PageIndexChanging" PageSize="12" AllowSorting="True" OnSorting="GridView_Sorting" >
9 <Columns>
10 <asp:BoundField DataField="UserID" HeaderText="UserID" SortExpression="UserID" />
11 <asp:BoundField DataField="C_Name" HeaderText="中文名字" SortExpression="C_Name" />
12 <asp:BoundField DataField="E_Name" HeaderText="英文名字" SortExpression="E_Name" />
13 <asp:BoundField DataField="QQ" HeaderText="QQ帐号" SortExpression="QQ" />
14 </Columns>
15 <RowStyle HorizontalAlign="Center" />
16 <PagerStyle HorizontalAlign="Right" />
17 </asp:GridView>
18 </td>
19 </tr>
20
21
22 </table>