</asp:TemplateField> 其中第一种方式会触发Sorting事件,第二种方式不会。所以如果要想为模板列提供排序的,要采用第一种方式指定标

一下是一个完整的列子

1. 一个GridView

首先: 将GridView的 AllowSorting 属性 设置为 true。(允许点击每一列的头来排序)

然后: 设置你想排序的列的 SortExpression="id" 为你要排序的数据源的相应属性。

<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False" onsorting="GridView1_Sorting">
<Columns>
<asp:TemplateField HeaderText="姓名" >
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("user.name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="id" SortExpression="id">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False" onsorting="GridView1_Sorting">
<Columns>
<asp:TemplateField HeaderText="姓名" >
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("user.name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="id" SortExpression="id">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
2. 完成代码

Java代码
public partial class mastermanage_CarManage : System.Web.UI.Page
/// <summary>
/// 查询出来的 car信息的list
/// </summary>
IEnumerable<Car> carList = null;
UserDao userDao = new UserDao();
protected void Page_Load(object sender, EventArgs e)
carList = userDao.getUserCar();
this.GridView1.DataSource = carList;
this.GridView1.DataBind();
/// <summary>
/// 模板列排序的方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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>
/// 这个 使用 linq帮定的
/// </summary>
private void BindGridView()
// 获取GridView排序数据列及排序方向
string sortExpression = this.GridView1.Attributes["SortExpression"];
string sortDirection = this.GridView1.Attributes["SortDirection"];
// 调用业务数据获取方法
UserDao userDao = new UserDao();
// 根据GridView排序数据列及排序方向设置显示的默认数据视图
if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
if (sortDirection.Equals("DESC"))
// l => l.id 这个应该按照 sortExpression的内容来写
//carList = from l in carList orderby l.id ascending select l;
carList = carList.OrderBy(l => l.id);
//carList = from l in carList orderby l.id descending select l;
carList = carList.OrderByDescending(l => l.id);
// GridView绑定并显示数据
this.GridView1.DataSource = carList.ToList<Car>();
this.GridView1.DataBind();

public partial class mastermanage_CarManage : System.Web.UI.Page
/// <summary>
/// 查询出来的 car信息的list
/// </summary>
IEnumerable<Car> carList = null;

UserDao userDao = new UserDao();

protected void Page_Load(object sender, EventArgs e)
carList = userDao.getUserCar();
this.GridView1.DataSource = carList;
this.GridView1.DataBind();

/// <summary>
/// 模板列排序的方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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>
/// 这个 使用 linq帮定的
/// </summary>
private void BindGridView()
// 获取GridView排序数据列及排序方向
string sortExpression = this.GridView1.Attributes["SortExpression"];
string sortDirection = this.GridView1.Attributes["SortDirection"];
// 调用业务数据获取方法
UserDao userDao = new UserDao();

// 根据GridView排序数据列及排序方向设置显示的默认数据视图
if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
if (sortDirection.Equals("DESC"))
// l => l.id 这个应该按照 sortExpression的内容来写
//carList = from l in carList orderby l.id ascending select l;
carList = carList.OrderBy(l => l.id);
//carList = from l in carList orderby l.id descending select l;
carList = carList.OrderByDescending(l => l.id);
// GridView绑定并显示数据
this.GridView1.DataSource = carList.ToList<Car>();
this.GridView1.DataBind();
/// <summary>
/// 绑定到 DataTable 数据源 排序
/// </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>
/// DataTable 数据源
/// </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"});
dt.Rows.Add(new object[] { "000002", "zhangyu"});
dt.Rows.Add(new object[] { "000003", "zhukundian"});
dt.Rows.Add(new object[] { "000004", "liyang"});
dt.Rows.Add(new object[] { "000005", "caili"});
return dt;
//********************************************************************************
//处理GridView的排序事件
protected void grv_Messenge_Sorting(object sender, GridViewSortEventArgs e)
string sortExpression = e.SortExpression;
if (GridViewSortDirection == SortDirection.Ascending) //设置排序方向
GridViewSortDirection = SortDirection.Descending;
SortGridView(sortExpression, " DESC");
GridViewSortDirection = SortDirection.Ascending;
SortGridView(sortExpression, " ASC");

//*******************************************************************************
/**//*
* 函数名:SortGridView,即对GridView进行排序
* 创建时间:2007年11月7日
* 功能描述:自定义GridView的排序方法,通过DataView中的排序方法对GridView的数据进行排序
* 输入参数:用于排序的关联表达式,排序的方向(升序或降序)
* 使用示例:SortGridView( sortExpression, "DESC")
* 返回值说明:无返回值
private void SortGridView(string sortExpression, string direction)
DataSet ds = GetData(); //查找数据源
DataTable dt = ds.Tables[0];

DataView dv = new DataView(dt);
dv.Sort = sortExpression + direction;
grv_Messenge.DataSource = dv; //将DataView绑定到GridView上
grv_Messenge.DataBind();