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)
??????? {
??????????? //頁面數(shù)據(jù)綁定
??????????? 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)
??????? {
??????????? //頁面數(shù)據(jù)綁定
??????????? 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)
??????? {
??????????? //頁面數(shù)據(jù)綁定
??????????? 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);
??? }
}
轉(zhuǎn)載于:https://www.cnblogs.com/82767136/articles/1383845.html
總結(jié)
以上是生活随笔為你收集整理的GridView自动排序(原创)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小功能 获取一段汉字的拼音首字母
- 下一篇: 程序员的10种级别