GridView正反雙向排序

iDotNetSpace發表於2008-09-26
  1. <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. nbsp;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. }

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-464493/,如需轉載,請註明出處,否則將追究法律責任。

相關文章