程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> ASP.NET中GridView正反雙向排序

ASP.NET中GridView正反雙向排序

編輯:.NET實例教程
效果圖:點姓名各2次的排序,點其他也一樣可以。

ASP.NET中GridView正反雙向排序

ASP.NET中GridView正反雙向排序

  後台代碼:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClIEnt;
public partial class Default3 : System.Web.UI.Page
{

//清清月兒的博客http://blog.csdn.Net/21ASPnet 
    SqlConnection sqlcon;
    string strCon = "Data Source=(local);Database=北風貿易;Uid=sa;Pwd=";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            VIEwState["SortOrder"] = "SFZ號碼";
            VIEwState["OrderDire"] = "ASC";
            bind();
        }
    }
    protected void GridView1_Sorting(object sender, GridVIEwSortEventArgs e)
    {
        string sPage = e.SortExpression;
        if (VIEwState["SortOrder"].ToString() == sPage)
        {
            if (VIEwState["OrderDire"].ToString() == "Desc")
                VIEwState["OrderDire"] = "ASC";
            else
                VIEwState["OrderDire"] = "Desc";
        }
        else
        {
            VIEwState["SortOrder"] = e.SortExpression;
        }
        bind();
    }

    public void bind()
    {
        
        string sqlstr = "select top 5 * from 飛狐工作室";
        sqlcon = new SqlConnection(strCon);
        SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
        DataSet myds = new DataSet();
        sqlcon.Open();
        myda.Fill(myds, "飛狐工作室");
        DataView view = myds.Tables["飛狐工作室"].DefaultVIEw;
        string sort = (string)ViewState["SortOrder"] + " " + (string)VIEwState["OrderDire"];
        vIEw.Sort = sort;
        GridView1.DataSource = vIEw;
        GridVIEw1.DataBind();
        sqlcon.Close();
    }
}

前台主要代碼:

<ASP:GridView ID="GridVIEw1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
                        CellPadding="3" Font-Size="9pt" OnSorting="GridVIEw1_Sorting" BackColor="White" BorderColor="#CCCCCC" Border BorderWidth="1px">
                        <FooterStyle BackColor="White" ForeColor="#000066" />
                        <Columns>
                             <ASP:BoundField DataFIEld="SFZ號碼" HeaderText="用戶ID" SortExpression="SFZ號碼" />
                            <ASP:BoundField DataFIEld="姓名" HeaderText="用戶姓名

" SortExpression="姓名"/>
                            <ASP:BoundField DataFIEld="員工性別" HeaderText="性別" SortExpression="員工性別"/>
                            <ASP:BoundField DataFIEld="家庭住址" HeaderText="家庭住址" SortExpression="家庭住址"/>
                                
                        </Columns>
                        <RowStyle ForeColor="#000066" />
                        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
                        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                    </ASP:GridVIEw>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved