源码网,源码论坛,源码之家,商业源码,游戏源码下载,discuz插件,棋牌源码下载,精品源码论坛

 找回密码
 立即注册
楼主: ttx9n

[ASP.NET] gridview自动排序示例分享

[复制链接]

7万

主题

861

回帖

32万

积分

论坛元老

Rank: 8Rank: 8

积分
329525
发表于 2014-1-15 10:56:50 | 显示全部楼层 |阅读模式
GridView自带了数据排序功能。在设计视图下,只能对GridView的排序数据列和排序方向进行静态设置。在后台程序中,则需要用Attributes方式对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" 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、设置OnSorting事件,对需要排序的列设定SortExpression属性。

后台

复制代码 代码如下:
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)
        {
            // 设定初始排序参数值

            // 错误的属性设置方法:SortExpression、SortDirection均是GridView只读属性,无法直接赋值。
            //this.GridView1.SortExpression = "id";
            //this.GridView1.SortDirection = "ASC";

            // 正确的属性设置方法
            this.GridView1.Attributes.Add("SortExpression", "id");
            this.GridView1.Attributes.Add("SortDirection", "ASC");

            // 绑定数据源到GridView
            this.BindGridView();
        }
    }

    /// <summary>
    /// GridView排序事件
    /// </summary>
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        // 从事件参数获取排序数据列
        string sortExpression = e.SortExpression.ToString();

        // 假定为排序方向为“顺序”
        string sortDirection = "ASC";

        // “ASC”与事件参数获取到的排序方向进行比较,进行GridView排序方向参数的修改
        if (sortExpression == this.GridView1.Attributes["SortExpression"])
        {
            //获得下一次的排序状态
            sortDirection = (this.GridView1.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
        }

        // 重新设定GridView排序数据列及排序方向
        this.GridView1.Attributes["SortExpression"] = sortExpression;
        this.GridView1.Attributes["SortDirection"] = sortDirection;

        this.BindGridView();
    }

    /// <summary>
    /// 绑定到GridView
    /// </summary>
    private void BindGridView()
    {
        // 获取GridView排序数据列及排序方向
        string sortExpression = this.GridView1.Attributes["SortExpression"];
        string sortDirection = this.GridView1.Attributes["SortDirection"];

        // 调用业务数据获取方法
        DataTable dtBind = this.getDB();

        // 根据GridView排序数据列及排序方向设置显示的默认数据视图
        if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
        {
            dtBind.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
        }

        // GridView绑定并显示数据
        this.GridView1.DataSource = dtBind;
        this.GridView1.DataBind();
    }

    /// <summary>
    /// 获取数据源的方法
    /// </summary>
    /// <returns>数据源</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;
    }
}

回复

使用道具 举报

0

主题

2万

回帖

61

积分

注册会员

Rank: 2

积分
61
发表于 2022-8-24 02:50:30 | 显示全部楼层
好人好人好人好人
回复 支持 反对

使用道具 举报

0

主题

2万

回帖

124

积分

注册会员

Rank: 2

积分
124
发表于 2022-10-14 06:36:26 | 显示全部楼层
啊,数码撒飒飒飒飒
回复 支持 反对

使用道具 举报

2

主题

2万

回帖

499

积分

中级会员

Rank: 3Rank: 3

积分
499
发表于 2023-3-28 06:17:24 | 显示全部楼层
那三门,你们谁寂寞才快乐撒
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2023-7-7 12:20:03 | 显示全部楼层
额UI废物iuhfujewfiewnnfen
回复 支持 反对

使用道具 举报

16

主题

2万

回帖

376

积分

中级会员

Rank: 3Rank: 3

积分
376
发表于 2023-7-9 09:52:59 | 显示全部楼层
额UI废物iuhfujewfiewnnfen
回复 支持 反对

使用道具 举报

27

主题

2万

回帖

331

积分

中级会员

Rank: 3Rank: 3

积分
331
发表于 2023-8-24 03:49:21 | 显示全部楼层
hi哦回复iOS就看见
回复 支持 反对

使用道具 举报

1

主题

2万

回帖

176

积分

注册会员

Rank: 2

积分
176
发表于 2023-9-9 05:21:59 | 显示全部楼层
看看看咋么
回复 支持 反对

使用道具 举报

11

主题

2万

回帖

300

积分

中级会员

Rank: 3Rank: 3

积分
300
发表于 2023-10-18 21:55:56 | 显示全部楼层
谢谢楼主分享
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2023-12-6 13:27:36 | 显示全部楼层
额风风风微风微风违法
回复 支持 反对

使用道具 举报

高级模式
B Color Image Link Quote Code Smilies

本版积分规则

手机版|小黑屋|网站地图|源码论坛 ( 海外版 )

GMT+8, 2025-2-3 03:56 , Processed in 0.066882 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表