GridView正反雙向排序
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- nbsp;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>GridView正反雙向排序title>
- head>
- <body>
- <form id="form1" runat="server">
- <div>
- <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">
- <FooterStyle BackColor="White" ForeColor="#000066" />
- <Columns>
- <asp:BoundField DataField="ID" HeaderText="使用者ID" ReadOnly="True" SortExpression="ID" />
- <asp:BoundField DataField="Name" HeaderText="使用者姓名" SortExpression="Name" />
- <asp:BoundField DataField="Sex" HeaderText="性別" SortExpression="Sex" />
- <asp:BoundField DataField="Address" HeaderText="家庭住址" SortExpression="Address" />
- 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>
- div>
- form>
- body>
- html>
- using System;
- using System.Data;
- using System.Data.SqlClient;
- using System.Configuration;
- 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;
- public partial class _Default : System.Web.UI.Page
- {
- SqlConnection sqlConn;
- SqlCommand sqlComm;
- string strConn = "Data Source=(local);Database=Exercise;Uid=sa;Pwd=sa";
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- ViewState["SortOrder"] = "ID";
- ViewState["OrderDire"] = "ASC";
- databind();
- }
- }
- private void databind()
- {
- string strSql = "select * from myDt";
- sqlConn = new SqlConnection(strConn);
- SqlDataAdapter sqlDa = new SqlDataAdapter(strSql, sqlConn);
- DataSet ds = new DataSet();
- sqlConn.Open();
- sqlDa.Fill(ds, "myDt");
- DataView dv = ds.Tables["myDt"].DefaultView;
- string strSort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
- dv.Sort = strSort;
- GridView1.DataSource = dv;
- GridView1.DataBind();
- sqlConn.Close();
- }
- protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
- {
- string strPage = e.SortExpression;
- if (ViewState["SortOrder"].ToString() == strPage)
- {
- if (ViewState["OrderDire"].ToString() == "Desc")
- ViewState["OrderDire"] = "ASC";
- else
- ViewState["OrderDire"] = "Desc";
- }
- else
- {
- ViewState["SortOrder"] = e.SortExpression;
- }
- databind();
- }
- }
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-464493/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 橫向Gridview使用View
- C#中實現DataGrid雙向排序(轉)C#排序
- 將二叉排序樹BST轉換成排序的雙向連結串列排序
- vue 雙向繫結(v-model 雙向繫結、.sync 雙向繫結、.sync 傳物件)Vue物件
- 雙向lstm原理
- 並行排序演算法:雙調排序並行排序演算法
- 微軟面試題,將二叉排序樹轉換成雙向連結串列微軟面試題排序
- 給datagrid控制元件建立穩固的雙向排序(asp.net) (轉)控制元件排序ASP.NET
- grpc雙向流RPC
- 雙向迭代介面:ListIterator
- UDP雙向通訊UDP
- 雙向連結串列
- java 雙向證書請求 雙向驗證機制 轉載Java
- Mysql中的雙路排序和單路排序MySql排序
- 【JS面試向】選擇排序、桶排序、氣泡排序和快速排序簡介JS面試排序
- 從單向到雙向資料繫結
- 實現listview控制元件report檢視下任何列頭的雙向排序 (轉)View控制元件排序
- Vue雙向繫結初探Vue
- 雙向通訊之websocketWeb
- 雙向通訊之SSE
- vue雙向繫結原理Vue
- GoldenGate雙向同步配置Go
- https雙向認證HTTP
- PostgreSQL雙向複製教程SQL
- HTTPS連線建立過程(單向&雙向)HTTP
- 改進的氣泡排序和選擇排序(雙向冒泡/一次遍歷選出最大和最小的選擇排序)C語言實現排序C語言
- 第三章 :查詢與排序-------3.4快排之雙向掃描分割槽法排序
- O(nlogn)快速排序-雙路排序+詳細註解排序
- 正反斜槓的區別
- vue雙向繫結的原理及實現雙向繫結MVVM原始碼分析VueMVVM原始碼
- Docker網路雙向連線Docker
- vue雙向繫結盲區Vue
- google guava的BiMap:雙向MapGoGuava
- JS雙向資料繫結JS
- Vue雙向繫結實現Vue
- 揭密 Vue 的雙向繫結Vue
- 實現雙向連結串列
- javascript中的雙向繫結JavaScript