DataGrid排序
DataGrid是ASP.NET中非常重要的一个控件。它能方便的让我们实现编辑、排序功能;但是排序功能默认的是升序(ASC),能不能让DataGrid同时实现升降序排列呢?这篇文章将给你一个比较好的解决方法。
下面的例子将告诉你如何给DataGrid动态添加sortexpression 和 sortdirection 属性,并通过DataView使DataGird中的数据按照这两个属性排列。在这个例子中使用的DataGrid的sortexpression属性只需要在sortcommand事件中设置(和通常的排序一样),DataGrid的sortexpression 属性保存了最后一次用来排序的字段名称,DataGrid的sortdirection 属性保存了最后一次用来排序的字段排列方式(“ASC”或者“DESC”)
页面设计:
1. 在页面上添加一个DataGrid;
2. 设置DataGrid的AllowSorting属性为True;
3. 设置AutogenerateColumns 属性为False;
4. 添加要绑定的字段到DataGrid,并按照下表设置其属性。Sortexpression属性和数据库中数据表中的字段名保持一致。
我使用Northwind数据库中的Employees来说明这个例子。
DataField (字段名) Header Text(显示的名称,解释) Sort Expression(字段名,排序用)
记得给datagrid绑定字段时候,设置上面三种属性
EmployeeID
Employee ID
EmployeeID
LastName
Last Name
LastName
FirstName
First Name
FirstName
Title
Title
Title
City
City
City
Country
Country
Country
程序代码设计:
1. 在页面第一次被加载时给DATAGRID添加动态属性sortexpression 和 sortdirection;
2. 给指定的字段的sortexpression 和 sortdirection 赋值,在加载时初始化DataGrid的排序字段和排序方式;
3. 得到dataset 和 dataview 对象。设置dataview 的sort 属性为动态属性sortexpression 和 sortdirection的连接字符串;
4. 用dataview绑定datagrid;
5. 当表头被点击时,触发datagrid的sortcommand事件;
6. 得到sortcommand事件传递过来的sortexpression属性,并与datagrid的sortexpression属性比较;
7. 如果找到相等的
a) 先检查sortdirection属性;
b) If sortdirection属性等于“ASC”then
i. 设置sortdirection属性的值等于“DESC”
c) Else
i. 设置sortdirection属性的值等于“ASC”
d) End If
8. 重复第3步
注意:当你运行下面的代码并点击同一列两次或者两次以上,此列的排列方式会自动根据现有的排列方式的反序排列。
下面的下面的代码将给你演示怎么使用DataGrid的动态属性和DataView对象。
C# Code:
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
if(DataGrid1.Attributes["SortExpression"] == null)
{
DataGrid1.Attributes["SortExpression"] = "LastName";
DataGrid1.Attributes["SortDirection"] = "ASC";
}
BindGrid();
}
}
private void BindGrid()
{
SqlConnection conn = new SqlConnection("server=localhost;uid=sa;pwd=sa;database=Northwind");
conn.Open();
SqlDataAdapter cmd = new SqlDataAdapter("select * from Employees",conn);
DataSet ds = new DataSet();
cmd.Fill(ds,"Employees");
DataView dv = new DataView();
dv = ds.Tables[0].DefaultView;
string SortExpression = DataGrid1.Attributes["SortExpression"];
string SortDirection = DataGrid1.Attributes["SortDirection"];
dv.Sort = SortExpression + " " + SortDirection;
DataGrid1.DataSource = dv;
DataGrid1.DataBind();
}
private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
string SortExpression = e.SortExpression.ToString();
string SortDirection = "ASC";
if(SortExpression == DataGrid1.Attributes["SortExpression"])
{
SortDirection = (DataGrid1.Attributes["SortDirection"].ToString() == SortDirection ? "DESC" : "ASC");
}
DataGrid1.Attributes["SortExpression"] = SortExpression;
DataGrid1.Attributes["SortDirection"] = SortDirection;
BindGrid();
}
DataGrid排序的更多相关文章
- easyUI datagrid 排序
easyUI datagrid 排序 1.首先设置datagrid属性sortName 2.设置sortOrder 3.设置remoteSort(注:此属性默认为true,如果如果是对本地数据排序必须 ...
- Silverlight datagrid 排序 (转)
Silverlight的DataGrid有很多强大之处,其中一个便是排序. DataGrid指定过ItemsSource之后,通过点击列头就可以实现排序,不用写任何代码.这对我这种懒人来说实在是太爽了 ...
- winform DataGrid排序、去掉第一的空白列
排序: dataGridView1.Sort(dataGridView1.Columns[3], ListSortDirection.Descending); 去掉空白列: dataGridView1 ...
- eayui datagrid 分页 排序 详解
最近因为经常使用easyui 在做表格时难免后出现排序 及分页的问题,但是 在官网中没有 相关的介绍及例子,所以经过多方面的查找后,终于完成了分页 和排序的功能 首先 页面datagrid 要排序的必 ...
- Datatable 筛选条件、排序 和获取datagrid当前页面 数据
1.转化为DataView进行筛选和排序 DataTable dt = BL.UserInfo();//查询返回的DataTable数据 DataTable dt2 = new DataTable( ...
- easyui datagrid 点击列表头排序出现错乱的原因
之前我的导师,也就是带我的同事,使用datagrid,发现点击列表头排序出现乱序,按理说只有顺序和逆序两种排序结果.因为他比较忙,当时没解决,把排序禁掉了,后来又要求一定要排序,所以他交给我. 一开始 ...
- 数据表格 - DataGrid - 字段排序
设置默认排序字段 sortName:"id",sortOrder:"desc",单独为每个字段设置排序 {field: "name", ti ...
- datagrid点击标题进行排序
步骤: 1.页面上首先设置datagrid的AllowSorting="true",以及指定排序方法OnSortCommand="DataGrid1_SortComman ...
- EasyUI DataGrid 添加排序
这个事例演示了如何在点击列头的时候排序DataGrid中全部的列可以通过点击列头被排序.你可以定义可以被排序的列.默认的,列不能被排序除非你设置sortable属性为TRUE,下面是例子:标记 < ...
随机推荐
- poj1182(食物链)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 49320 Accepted: 14385 Description ...
- HDOJ 1596
9899828 2013-12-27 16:42:37 Accepted 1596 3312MS 6668K 711 B C++ 泽泽 floyed暴力 #include<cstdio> ...
- HTML前端
1.<html>内容</html> 解释:HTML文档的文档标记,也成为HTML开始标记 功能:这对标记分别位于网页的最前端和最后端 <html>在最前段表示网页的 ...
- MySQL建立索引的注意事项
对于大数据量的表格,尤其是百万行以上的数据表,一定要对其建立索引,否则查询速度极慢.(参考后面的测试结果)建立索引时需注意: MySQL的索引有两种:单列索引(即在某一列上建索引).多列组合索引(即在 ...
- String to Integer
Implement function atoi to convert a string to an integer. If no valid conversion could be performed ...
- yum缓存配置
引自:http://www.cnblogs.com/xiaochaohuashengmi/archive/2011/10/09/2203916.html $ cat /etc/yum.conf ...
- Java for LeetCode 072 Edit Distance【HARD】
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- 电话连线(codevs 1003)
题目描述 Description 一个国家有n个城市.若干个城市之间有电话线连接,现在要增加m条电话线(电话线当然是双向的了),使得任意两个城市之间都直接或间接经过其他城市有电话线连接,你的程序应该能 ...
- Cocos2dx实现象棋之布局
开始界面 #ifndef SCENESTART_H #define SCENESTART_H #include "cocos2d.h" #include "SceneGa ...
- iptables 无法连外网
[root@v01-svn-test-server ~]# service iptables status Table: filter Chain INPUT (policy DROP) num ta ...