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


本文转载:CSDN博客