後台代碼:
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;
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>