本文转自:http://www.cnblogs.com/eva_2010/articles/1995646.html

在GridView中,根据其中的某列进行排序。

1. 页面:AllowSorting=“True” onsorting=“ ”,SortExpression="列名"

<asp:GridView ID="grdResult" runat="server" CssClass="gridviewstyle" AutoGenerateColumns="False" 

AllowSorting="True" onsorting="grdResult_Sorting">

         <Columns>
<asp:CommandField HeaderText="Edit" ShowEditButton="True">
<ControlStyle Width="150px" />
</asp:CommandField>
<asp:BoundField DataField="Id" HeaderText="ID" SortExpression="Id">
<ControlStyle Width="20px" />
<HeaderStyle Width="80px" ForeColor="White" />
</asp:BoundField>
<asp:BoundField DataField="Name" HeaderText="Name" >
<ControlStyle Width="50px" />
<HeaderStyle Width="120px" Font-Underline="True" />
</asp:BoundField>
</Columns> </asp:GridView>

2.后台代码:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.grdResult.Attributes.Add("SortExpression", "Id");
this.grdResult.Attributes.Add("SortDirection", "ASC");
BindGridInfo();
}
}
private List<Test> GetTestData()
{
Test test1 = new Test { Id = , Name = "Test1" };
Test test2 = new Test { Id = , Name = "Test2" };
Test test3 = new Test { Id = , Name = "Test3" };
Test test4 = new Test { Id = , Name = "Test4" };
List<Test> lstTest = new List<Test>();
lstTest.Add(test1);
lstTest.Add(test2);
lstTest.Add(test3);
lstTest.Add(test4);
return lstTest;
}
private DataTable GetData()
{
DataTable dtTest = new DataTable();
dtTest.Columns.Add("Id");
dtTest.Columns.Add("Name");
dtTest.Rows.Add(, "");
dtTest.Rows.Add(, "");
dtTest.Rows.Add(, "");
dtTest.Rows.Add(, ""); return dtTest;
}
//数据绑定,如果返回数据源是DataTable则可以直接排序,如果不是则要先转换为DataTable格式数据源
private void BindGridInfo()
{
List<Test> lstTest = GetTestData();
DataTable dt = new DataTable();
dt.Columns.Add("Id");
dt.Columns.Add("Name");
for (int i = ; i < lstTest.Count; i++)
{
DataRow dr = dt.NewRow();
dr["Id"] = lstTest[i].Id;
dr["Name"] = lstTest[i].Name;
dt.Rows.Add(dr);
}
string sortExpression = this.grdResult.Attributes["SortExpression"];
string sortDirection = this.grdResult.Attributes["SortDirection"]; DataTable dtSource = GetData();
if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
{
dt.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
}
grdResult.DataSource = dt;// dtSource;
grdResult.DataBind();
}
Class Test:  

    /// <summary>
/// define a container class
/// </summary>
private class Test
{
private int _id;
/// <summary>
/// test id
/// </summary>
public int Id
{
get { return _id; }
set { _id = value; }
}
private string _name;
/// <summary>
/// test name
/// </summary>
public string Name
{
get { return _name; }
set { _name = value; }
}
}
/// <summary>
/// sorting
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void grdResult_Sorting(object sender, GridViewSortEventArgs e)
{
string sortExpression = e.SortExpression.ToString();
string sortDirection = "ASC";
if (sortExpression == this.grdResult.Attributes["SortExpression"])
{
sortDirection = (this.grdResult.Attributes["SortDirection"].ToString() == sortDirection) ? "DESC" : "ASC";
}
this.grdResult.Attributes["SortExpression"] = sortExpression;
this.grdResult.Attributes["SortDirection"] = sortDirection;
this.BindGridInfo();
}

参考: http://www.cnblogs.com/heekui/archive/2008/06/02/1212051.html

Be the change you want to see in the world.

[转]GridView排序——微软提供Sort的更多相关文章

  1. 简单选择排序(Simple Selection Sort)

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  2. 排序合并连接(sort merge join)的原理

    排序合并连接(sort merge join)的原理 排序合并连接(sort merge join)的原理     排序合并连接(sort merge join)       访问次数:两张表都只会访 ...

  3. 使用UI Automation实现自动化测试 --微软提供的控件Pattern

    微软提供的控件Pattern System.Windows.Automation 命名空间 System.Windows.Automation.BasePattern 为控件模式类提供基实现 Syst ...

  4. [大牛翻译系列]Hadoop(5)MapReduce 排序:次排序(Secondary sort)

    4.2 排序(SORT) 在MapReduce中,排序的目的有两个: MapReduce可以通过排序将Map输出的键分组.然后每组键调用一次reduce. 在某些需要排序的特定场景中,用户可以将作业( ...

  5. 微软提供的API的各个版本之间的区别

    First Floor Software这个diff lists非常方便的给出了微软提供的API的各个版本之间的区别,比如下表是.NET 4和.NET 4.5的API变化总结.我们可以看到.NET 4 ...

  6. 微软提供了三个核心服务:Windows+Office 365+Azure

    微软提供了三个核心服务:Windows+Office 365+Azure 英语新闻来源:http://techcrunch.com/2014/11/10/microsofts-ceo-breaks-d ...

  7. 《算法4》2.1 - 选择排序算法(Selection Sort), Python实现

    选择排序算法(Selection Sort)是排序算法的一种初级算法.虽然比较简单,但是基础,理解了有助于后面学习更高深算法,勿以勿小而不为. 排序算法的语言描述: 给定一组物体,根据他们的某种可量化 ...

  8. 编写一个类,其中包含一个排序的方法Sort(),当传入的是一串整数,就按照从小到大的顺序输出,如果传入的是一个字符串,就将字符串反序输出。

    namespace test2 { class Program { /// <summary> /// 编写一个类,其中包含一个排序的方法Sort(),当传入的是一串整数,就按照从小到大的 ...

  9. 简单选择排序 Selection Sort 和树形选择排序 Tree Selection Sort

    选择排序 Selection Sort 选择排序的基本思想是:每一趟在剩余未排序的若干记录中选取关键字最小的(也可以是最大的,本文中均考虑排升序)记录作为有序序列中下一个记录. 如第i趟选择排序就是在 ...

随机推荐

  1. 重新想象 Windows 8 Store Apps (39) - 契约: Share Contract

    [源码下载] 重新想象 Windows 8 Store Apps (39) - 契约: Share Contract 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之  ...

  2. 验证控件插图扩展控件ValidatorCalloutExtender(用于扩展验证控件)和TextBoxWatermarkExtender

    <asp:ScriptManager ID="ScriptManager1" runat="server">  </asp:ScriptMan ...

  3. SSM框架整合总结

    关于ssm整合的相关总结: 1.持久层--->mybatis:通过Spring 来管理持久层的 Mapper (相当于 dao 接口),来完成对数据库的操作. 首先我们回顾一下,在单独使用myb ...

  4. LCA算法倍增算法(洛谷3379模板题)

    倍增(爬树)算法,刚刚学习的算法.对每一个点的父节点,就记录他的2k的父亲. 题目为http://www.luogu.org/problem/show?pid=3379 第一步先记录每一个节点的深度用 ...

  5. C++之多态的一个例子

    [例12.1] 先建立一个Point(点)类,包含数据成员x,y(坐标点).以它为基类,派生出一个Circle(圆)类,增加数据成员r(半径),再以Circle类为直接基类,派生出一个Cylinder ...

  6. Visual Studio添加dll程序集引用操作步骤

    Visual Studio 中添加引用的操作: 在“解决方案资源管理器”中,先右击项目图标,在弹出菜单选择“添加引用...” 然后在弹出的窗口中选择所要添加的选项,点击确定就可以了. 原文:http: ...

  7. sharepoint 顺序工作流创建

    顺序工作流提供了一系列有组织的步骤,一般情况下,步骤是逐一执行的. 1.新建 > 项目,选择 SharePoint解决方案 > 空项目: 2.部署为场解决方案 3.添加 > 新项,选 ...

  8. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q51-Q53)

    Question 51You use a third-party site definition to create SharePoint sites.You need to add a Web Pa ...

  9. 安装并运行hadoop

    本文地址:http://www.cnblogs.com/archimedes/p/run-hadoop.html,转载请注明源地址. 欢迎关注我的个人博客:www.wuyudong.com, 更多云计 ...

  10. Spring中配置数据源的4种形式(转)

    原文http://blog.csdn.net/orclight/article/details/8616103       不管采用何种持久化技术,都需要定义数据源.Spring中提供了4种不同形式的 ...