最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

gridview自動排序示例分享

 更新時間:2014年01月15日 10:56:50   作者:  
GridView自帶了數(shù)據(jù)排序功能。在設(shè)計視圖下,只能對GridView的排序數(shù)據(jù)列和排序方向進(jìn)行靜態(tài)設(shè)置。在后臺程序中,則需要用Attributes方式對GridView的這兩個屬性進(jìn)行動態(tài)設(shè)置

示例如下:前臺

復(fù)制代碼 代碼如下:

<%@ 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>無標(biāo)題頁</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" AllowSorting="True" OnSorting="GridView1_Sorting">
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#EFF3FB" />
            <Columns>
                <asp:BoundField DataField="id" HeaderText="ID" SortExpression="id" />
                <asp:BoundField DataField="name" HeaderText="NAME" SortExpression="name" />
                <asp:BoundField DataField="age" HeaderText="AGE" SortExpression="age" />
            </Columns>
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>

前臺注意點:
需要對GridView啟用AllowSorting、設(shè)置OnSorting事件,對需要排序的列設(shè)定SortExpression屬性。

后臺

復(fù)制代碼 代碼如下:

using System;
using System.Data;
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
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // 設(shè)定初始排序參數(shù)值

            // 錯誤的屬性設(shè)置方法:SortExpression、SortDirection均是GridView只讀屬性,無法直接賦值。
            //this.GridView1.SortExpression = "id";
            //this.GridView1.SortDirection = "ASC";

            // 正確的屬性設(shè)置方法
            this.GridView1.Attributes.Add("SortExpression", "id");
            this.GridView1.Attributes.Add("SortDirection", "ASC");

            // 綁定數(shù)據(jù)源到GridView
            this.BindGridView();
        }
    }

    /// <summary>
    /// GridView排序事件
    /// </summary>
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        // 從事件參數(shù)獲取排序數(shù)據(jù)列
        string sortExpression = e.SortExpression.ToString();

        // 假定為排序方向為“順序”
        string sortDirection = "ASC";

        // “ASC”與事件參數(shù)獲取到的排序方向進(jìn)行比較,進(jìn)行GridView排序方向參數(shù)的修改
        if (sortExpression == this.GridView1.Attributes["SortExpression"])
        {
            //獲得下一次的排序狀態(tài)
            sortDirection = (this.GridView1.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
        }

        // 重新設(shè)定GridView排序數(shù)據(jù)列及排序方向
        this.GridView1.Attributes["SortExpression"] = sortExpression;
        this.GridView1.Attributes["SortDirection"] = sortDirection;

        this.BindGridView();
    }

    /// <summary>
    /// 綁定到GridView
    /// </summary>
    private void BindGridView()
    {
        // 獲取GridView排序數(shù)據(jù)列及排序方向
        string sortExpression = this.GridView1.Attributes["SortExpression"];
        string sortDirection = this.GridView1.Attributes["SortDirection"];

        // 調(diào)用業(yè)務(wù)數(shù)據(jù)獲取方法
        DataTable dtBind = this.getDB();

        // 根據(jù)GridView排序數(shù)據(jù)列及排序方向設(shè)置顯示的默認(rèn)數(shù)據(jù)視圖
        if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
        {
            dtBind.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
        }

        // GridView綁定并顯示數(shù)據(jù)
        this.GridView1.DataSource = dtBind;
        this.GridView1.DataBind();
    }

    /// <summary>
    /// 獲取數(shù)據(jù)源的方法
    /// </summary>
    /// <returns>數(shù)據(jù)源</returns>
    private DataTable getDB()
    {
        DataTable dt = new DataTable();

        dt.Columns.Add("id");
        dt.Columns.Add("name");
        dt.Columns.Add("age");

        dt.Rows.Add(new object[] { "000001", "hekui", "26" });
        dt.Rows.Add(new object[] { "000002", "zhangyu", "26" });
        dt.Rows.Add(new object[] { "000003", "zhukundian", "27" });
        dt.Rows.Add(new object[] { "000004", "liyang", "25" });
        dt.Rows.Add(new object[] { "000005", "caili", "27" });

        return dt;
    }
}

相關(guān)文章

最新評論

九龙县| 婺源县| 马山县| 盐山县| 华蓥市| 密山市| 湛江市| 普兰县| 垣曲县| 阿荣旗| 章丘市| 民丰县| 陆河县| 红安县| 江都市| 榕江县| 南华县| 麟游县| 色达县| 五寨县| 宕昌县| 金塔县| 德江县| 荆州市| 扎鲁特旗| 武定县| 醴陵市| 屏南县| 潞城市| 昌黎县| 建阳市| 共和县| 论坛| 固始县| 藁城市| 恩平市| 图片| 大宁县| 东丽区| 赞皇县| 马鞍山市|