GridView实现自动排序带上下箭头

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewSort.aspx.cs" Inherits="GridViewSort" %>

<!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 id="Head1" runat="server">
    <title>GridView排序</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
                    ForeColor="#333333" GridLines="None" AllowSorting="True" OnRowCreated="GridView1_RowCreated"
                    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>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class GridViewSort : System.Web.UI.Page
{
    #region 私有变量
    protected string JS = "";
    string SortExpression = "";//排序表达式
    string SortDirection = "";//排序方向
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridView();
        }
    }

    #region 事件
    /// <summary>
    /// 生成排序列旁边的小箭头
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            foreach (TableCell tc in e.Row.Cells)
            {
                if (tc.Controls.Count > 0)//这里要判断一下此时是不是已经生成了linkbutton
                {
                    string s1 = ((LinkButton)tc.Controls[0]).Text;
                    ((LinkButton)tc.Controls[0]).Text = s1.Replace(s1, s1 + "<font face='Webdings'>5</font>");
                }
                if (tc.Controls.Count > 0 && tc.Controls[0].GetType().ToString() == "System.Web.UI.WebControls.DataControlLinkButton")
                {
                    if (((LinkButton)tc.Controls[0]).CommandArgument == SortExpression)
                    {
                        string s2 = ((LinkButton)tc.Controls[0]).Text;
                        if (SortDirection == "DESC")
                        {
                            ((LinkButton)tc.Controls[0]).Text = s2.Replace("5", "6");
                        }
                    }
                }
            }
        }
    }
    /// <summary>
    /// 排序方法
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        //得到排序条件
        SortExpression = e.SortExpression.ToString();
        //设置排序为升序
        SortDirection = "ASC";
        //当前排序为升序时,修改成降序
        if (this.GridView1.Attributes["SortDirection"] == "ASC")
        {
            SortDirection = "DESC";
        }
        //设置GridView的排序状态
        this.GridView1.Attributes["SortExpression"] = SortExpression;
        this.GridView1.Attributes["SortDirection"] = SortDirection;
        BindGridView();
    }
    #endregion

    #region 私有方法
    /// <summary>
    /// 根据排序条件重新绑定GridView
    /// </summary>
    private void BindGridView()
    {
        string sortExpression = this.GridView1.Attributes["SortExpression"];
        string sortDirection = this.GridView1.Attributes["SortDirection"];
        DataTable dt = getDB();
        if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
        {
            dt.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
        }
        this.GridView1.DataSource = dt;
        this.GridView1.DataBind();
    }
    #endregion

    /// <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;
    }
}

转载于:https://www.cnblogs.com/smartsmile/archive/2012/11/30/6234357.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值