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,下面是例子:标记 < ...
随机推荐
- show processlist 其中status详解(适用于所有概况)
mysql show processlist分析 2011-04-11 16:13:00 分类: Mysql/postgreSQL mysql> show processlist; +—–+—— ...
- django revision
由于多次涉及到了这个东东,又不是很理解机制,决定深入研究下. what django-revision到底啥玩意?readthedocs上只有一句话概括:django-reversion can be ...
- 【Network】一张图看懂 Reactor 与 Proactor 模型的区别
首先来看看Reactor模式,Reactor模式应用于同步I/O的场景.我们以读操作为例来看看Reactor中的具体步骤:读取操作:1. 应用程序注册读就需事件和相关联的事件处理器2. 事件分离器等待 ...
- 关于Linux下进程间使用共享内存和信号量通信的时的编译问题
今天在编译一个使用信号量实现进程同步时,出现了库函数不存在的问题.如下图 编译结果实际上是说,没include相应的头文件,或是头文件不存在(即系统不支持该库函数) 但我man shm_open是可以 ...
- Java for LeetCode 199 Binary Tree Right Side View
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- codeforces B. Xenia and Ringroad 解题报告
题目链接:http://codeforces.com/problemset/problem/339/B 题目理解不难,这句是解题的关键 In order to complete the i-th ta ...
- HDU1850 Being a Good Boy in Spring Festival(博弈)
Being a Good Boy in Spring Festival Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I ...
- Android-- FragmentStatePagerAdapter分页(转载)
转载地址:http://blog.csdn.net/dreamzml/article/details/9951577 ViewPager ViewPager 如其名所述,是负责翻页的一个 View.准 ...
- Maven使用笔记(七)Maven使用问题记录
1.Java-maven异常-cannot be cast to javax.servlet.Filter 报错 tomcat 启动后先将tomcat/lib目录下的jar包全部读入内存,如果weba ...
- VIM Tutorials----(updating)
This is my first English tutorial; I hope I can help you to learn VIM. Any question send email to me ...