[转]GridView排序——微软提供Sort
本文转自: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
[转]GridView排序——微软提供Sort的更多相关文章
- 简单选择排序(Simple Selection Sort)
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- 排序合并连接(sort merge join)的原理
排序合并连接(sort merge join)的原理 排序合并连接(sort merge join)的原理 排序合并连接(sort merge join) 访问次数:两张表都只会访 ...
- 使用UI Automation实现自动化测试 --微软提供的控件Pattern
微软提供的控件Pattern System.Windows.Automation 命名空间 System.Windows.Automation.BasePattern 为控件模式类提供基实现 Syst ...
- [大牛翻译系列]Hadoop(5)MapReduce 排序:次排序(Secondary sort)
4.2 排序(SORT) 在MapReduce中,排序的目的有两个: MapReduce可以通过排序将Map输出的键分组.然后每组键调用一次reduce. 在某些需要排序的特定场景中,用户可以将作业( ...
- 微软提供的API的各个版本之间的区别
First Floor Software这个diff lists非常方便的给出了微软提供的API的各个版本之间的区别,比如下表是.NET 4和.NET 4.5的API变化总结.我们可以看到.NET 4 ...
- 微软提供了三个核心服务:Windows+Office 365+Azure
微软提供了三个核心服务:Windows+Office 365+Azure 英语新闻来源:http://techcrunch.com/2014/11/10/microsofts-ceo-breaks-d ...
- 《算法4》2.1 - 选择排序算法(Selection Sort), Python实现
选择排序算法(Selection Sort)是排序算法的一种初级算法.虽然比较简单,但是基础,理解了有助于后面学习更高深算法,勿以勿小而不为. 排序算法的语言描述: 给定一组物体,根据他们的某种可量化 ...
- 编写一个类,其中包含一个排序的方法Sort(),当传入的是一串整数,就按照从小到大的顺序输出,如果传入的是一个字符串,就将字符串反序输出。
namespace test2 { class Program { /// <summary> /// 编写一个类,其中包含一个排序的方法Sort(),当传入的是一串整数,就按照从小到大的 ...
- 简单选择排序 Selection Sort 和树形选择排序 Tree Selection Sort
选择排序 Selection Sort 选择排序的基本思想是:每一趟在剩余未排序的若干记录中选取关键字最小的(也可以是最大的,本文中均考虑排升序)记录作为有序序列中下一个记录. 如第i趟选择排序就是在 ...
随机推荐
- 如何弹出一定的大小的web窗体?
如何弹出一定的大小的web窗体? 摘自: http://blog.163.com/hweibin126@126/blog/static/17044246920108413348344/ 一.wind ...
- sencha gridpanel改变单元格颜色
标题列包含 审核通过则绿色,包含拒绝为红色: { xtype: 'gridcolumn', renderer: function(value, metaData, record, rowIndex, ...
- linux下安装或升级GCC4.8,以支持C++11标准
C++11标准在2011年8月份获得一致通过,这是自1998年后C++语言第一次大修订,对C++语言进行了改进和扩充.随后各编译器厂商都各自实现或部分实现了C++中的特性. 如需查看各编译器对C++1 ...
- 才知道百度也提供了智能DNS服务 - 加速乐
http://jiasule.baidu.com/ 智能DNS 依托百度多年积累的高精度DNS识别库,平均只需5秒全球DNS服务器全部生效,百度蜘蛛1秒生效.抗攻击.无限解析记录,免费支持电信.联通. ...
- Mysql的简单使用(三)
接上文Mysql的简单使用(二) mysql中结构相同的两个表进行合并:(注意需要两个表的结构是一样的) 有如下结构的两个表father和person. 合并的步骤为: 1.把person表和fath ...
- [js开源组件开发]js多选日期控件
js多选日期控件 详情请见:http://www.lovewebgames.com/jsmodule/calendar.html 它的github地址:https://github.com/tianx ...
- Angular 核心概念
module(模块) 作用 通过模块对页面进行业务上的划分,根据不同的功能划分不同的模块. 将重复使用的指令或者过滤器之类的代码做成模块,方便复用 注意必须指定第二个参数,否则变成找到已经定义的模块 ...
- sqlserver 存储过程 try catch TRANSACTION (转)
CREATE PROCEDURE YourProcedure ASBEGIN SET NOCOUNT ON; BEGIN TRY---------------------开始捕捉异常 ...
- Javascript对象赋值操作
首先,我们还是举个例子来说明对象赋值操作的问题吧: ps: 本文默认约定log = console.log function A(){} A.prototype.x = 10; var a1 = ne ...
- 微信CRM六大模块的详解
微信团队一直强调企业微信的主要功能是服务而非营销工具,微信5.0将公众号区分为服务号和订阅号,10月底平台为服务号开放高级接口,包括客服接口.网页授权等,可见服务是微信公众号的核心价值和方向.前一阵很 ...