GridView正反双向排序

  1. <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head runat="server">
  5.     <title>GridView正反双向排序</title>
  6. </head>
  7. <body>
  8.     <form id="form1" runat="server">
  9.     <div>
  10.         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="10" ForeColor="#333333" PageSize="3" AllowSorting="True" BackColor="White" BorderColor="#CCCCCC" Font-Size="9pt" OnSorting="GridView1_Sorting" BorderStyle="None" BorderWidth="1px"> 
  11.             <FooterStyle BackColor="White" ForeColor="#000066" />
  12.             <Columns>
  13.                 <asp:BoundField DataField="ID" HeaderText="用户ID" ReadOnly="True" SortExpression="ID" />
  14.                 <asp:BoundField DataField="Name" HeaderText="用户姓名" SortExpression="Name" />
  15.                 <asp:BoundField DataField="Sex" HeaderText="性别" SortExpression="Sex" />
  16.                 <asp:BoundField DataField="Address" HeaderText="家庭住址" SortExpression="Address" />
  17.             </Columns>
  18.             <RowStyle ForeColor="#000066" />
  19.             <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
  20.             <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
  21.             <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
  22.         </asp:GridView>
  23.     </div>
  24.     </form>
  25. </body>
  26. </html>
  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. using System.Configuration;
  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. public partial class _Default : System.Web.UI.Page 
  12. {
  13.     SqlConnection sqlConn;
  14.     SqlCommand sqlComm;
  15.     string strConn = "Data Source=(local);Database=Exercise;Uid=sa;Pwd=sa";
  16.     protected void Page_Load(object sender, EventArgs e)
  17.     {
  18.         if (!IsPostBack)
  19.         {
  20.             ViewState["SortOrder"] = "ID";
  21.             ViewState["OrderDire"] = "ASC";
  22.             databind();
  23.         }
  24.     }
  25.     private void databind()
  26.     {
  27.         string strSql = "select * from myDt";
  28.         sqlConn = new SqlConnection(strConn);
  29.         SqlDataAdapter sqlDa = new SqlDataAdapter(strSql, sqlConn);
  30.         DataSet ds = new DataSet();
  31.         sqlConn.Open();
  32.         sqlDa.Fill(ds, "myDt");
  33.         DataView dv = ds.Tables["myDt"].DefaultView;
  34.         string strSort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
  35.         dv.Sort = strSort;
  36.         GridView1.DataSource = dv;
  37.         GridView1.DataBind();
  38.         sqlConn.Close();
  39.     }
  40.     protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
  41.     {
  42.         string strPage = e.SortExpression;
  43.         if (ViewState["SortOrder"].ToString() == strPage)
  44.         {
  45.             if (ViewState["OrderDire"].ToString() == "Desc")
  46.                 ViewState["OrderDire"] = "ASC";
  47.             else
  48.                 ViewState["OrderDire"] = "Desc";
  49.         }
  50.         else
  51.         {
  52.             ViewState["SortOrder"] = e.SortExpression;
  53.         }
  54.         databind();
  55.     }
  56. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值