GridView控件自带排序(一)

来源:互联网 发布:淘宝香港官网 编辑:程序博客网 时间:2024/05/17 06:41

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 }
69
70

 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>
 

原创粉丝点击