欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

gridview自动排序示例分享

程序员文章站 2024-02-28 09:03:52
示例如下:前台 复制代码 代码如下:<%@ page language="c#" autoeventwireup="true"  codefile="de...

示例如下:前台

复制代码 代码如下:

<%@ 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;
    }
}