GridView自动排序(原创)
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" Height="190px" Width="589px" AllowSorting="True" AutoGenerateColumns="False" OnSorting="GridView1_Sorting">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="Id" />
<asp:BoundField DataField="Name" HeaderText=" 姓名" SortExpression="Name" />
<asp:BoundField DataField="Age" HeaderText=" 年龄" SortExpression="Age" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
后台:
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 _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//页面数据绑定
GetView("id");
}
}
protected void GetView(string sort)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.AppSettings["MyConn"];
try
{
con.Open();
string sql = "select * from xs";
SqlDataAdapter com = new SqlDataAdapter(sql, con);
DataSet dst = new DataSet();
com.Fill(dst,"xs");
DataView dv = dst.Tables["xs"].DefaultView;
if (sort == "")
{
dv.Sort = " id Desc";
}
else
{
dv.Sort = sort + " Desc";
}
GridView1.DataSource = dv;
GridView1.DataBind();
}
catch (Exception ee)
{
throw ee;
}
finally { con.Close(); }
}
//protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
//{
// this.GridView1.PageIndex = e.NewPageIndex;
// GetView(ViewState["SortEx"].ToString());
//}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
ViewState["SortEx"] = e.SortExpression;
GetView(e.SortExpression);
}
}
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 _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//页面数据绑定
GetView("id");
}
}
protected void GetView(string sort)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.AppSettings["MyConn"];
try
{
con.Open();
string sql = "select * from xs";
SqlDataAdapter com = new SqlDataAdapter(sql, con);
DataSet dst = new DataSet();
com.Fill(dst,"xs");
DataView dv = dst.Tables["xs"].DefaultView;
if (sort == "")
{
dv.Sort = " id Desc";
}
else
{
dv.Sort = sort + " Desc";
}
GridView1.DataSource = dv;
GridView1.DataBind();
}
catch (Exception ee)
{
throw ee;
}
finally { con.Close(); }
}
//protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
//{
// this.GridView1.PageIndex = e.NewPageIndex;
// GetView(ViewState["SortEx"].ToString());
//}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
ViewState["SortEx"] = e.SortExpression;
GetView(e.SortExpression);
}
}
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 _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//页面数据绑定
GetView("id");
}
}
protected void GetView(string sort)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.AppSettings["MyConn"];
try
{
con.Open();
string sql = "select * from xs";
SqlDataAdapter com = new SqlDataAdapter(sql, con);
DataSet dst = new DataSet();
com.Fill(dst,"xs");
DataView dv = dst.Tables["xs"].DefaultView;
if (sort == "")
{
dv.Sort = " id Desc";
}
else
{
dv.Sort = sort + " Desc";
}
GridView1.DataSource = dv;
GridView1.DataBind();
}
catch (Exception ee)
{
throw ee;
}
finally { con.Close(); }
}
//protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
//{
// this.GridView1.PageIndex = e.NewPageIndex;
// GetView(ViewState["SortEx"].ToString());
//}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
ViewState["SortEx"] = e.SortExpression;
GetView(e.SortExpression);
}
}