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排序的更多相关文章

  1. easyUI datagrid 排序

    easyUI datagrid 排序 1.首先设置datagrid属性sortName 2.设置sortOrder 3.设置remoteSort(注:此属性默认为true,如果如果是对本地数据排序必须 ...

  2. Silverlight datagrid 排序 (转)

    Silverlight的DataGrid有很多强大之处,其中一个便是排序. DataGrid指定过ItemsSource之后,通过点击列头就可以实现排序,不用写任何代码.这对我这种懒人来说实在是太爽了 ...

  3. winform DataGrid排序、去掉第一的空白列

    排序: dataGridView1.Sort(dataGridView1.Columns[3], ListSortDirection.Descending); 去掉空白列: dataGridView1 ...

  4. eayui datagrid 分页 排序 详解

    最近因为经常使用easyui 在做表格时难免后出现排序 及分页的问题,但是 在官网中没有 相关的介绍及例子,所以经过多方面的查找后,终于完成了分页 和排序的功能 首先 页面datagrid 要排序的必 ...

  5. Datatable 筛选条件、排序 和获取datagrid当前页面 数据

    1.转化为DataView进行筛选和排序  DataTable dt = BL.UserInfo();//查询返回的DataTable数据 DataTable dt2 = new DataTable( ...

  6. easyui datagrid 点击列表头排序出现错乱的原因

    之前我的导师,也就是带我的同事,使用datagrid,发现点击列表头排序出现乱序,按理说只有顺序和逆序两种排序结果.因为他比较忙,当时没解决,把排序禁掉了,后来又要求一定要排序,所以他交给我. 一开始 ...

  7. 数据表格 - DataGrid - 字段排序

    设置默认排序字段 sortName:"id",sortOrder:"desc",单独为每个字段设置排序 {field: "name", ti ...

  8. datagrid点击标题进行排序

    步骤: 1.页面上首先设置datagrid的AllowSorting="true",以及指定排序方法OnSortCommand="DataGrid1_SortComman ...

  9. EasyUI DataGrid 添加排序

    这个事例演示了如何在点击列头的时候排序DataGrid中全部的列可以通过点击列头被排序.你可以定义可以被排序的列.默认的,列不能被排序除非你设置sortable属性为TRUE,下面是例子:标记 < ...

随机推荐

  1. Coursera台大机器学习技法课程笔记04-Soft-Margin Support Vector Machine

    之前的SVM非常的hard,要求每个点都要被正确的划分,这就有可能overfit,为此引入了Soft SVM,即允许存在被错分的点,将犯的错放在目 标函数中进行优化,非常类似于正则化. 将Soft S ...

  2. poj2993 翻转2996

    Emag eht htiw Em Pleh Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2944   Accepted:  ...

  3. 组合数学or not ---- n选k有重

    模板问题: 1. 取物品 (comb.pas/c/cpp) [问题描述] 现在有n个物品(有可能相同),请您编程计算从中取k个有多少种不同的取法.[输入] 输入文件有两行,第一行包含两个整数n,k(2 ...

  4. ubuntu14.04 中国源

    deb http://cn.archive.ubuntu.com/ubuntu/ trusty main restricted universe multiversedeb http://cn.arc ...

  5. 《ASP.NET1200例》<asp:DataList>分页显示图片

    aspx页面代码 <asp:DataList ID="dlPhoto" runat="server" Height="137px" W ...

  6. Reverse Linked List | & ||

    Reverse Linked List I Reverse a linked list. Example For linked list 1->2->3, the reversed lin ...

  7. Linux服务器通过rz/sz轻松上传下载文件

    Linux服务器通过命令行远程访问时,上传文件还需要ftp所以不太方便,可以使用rz这个小工具来上传不太大的文件,方法如下: 输入rz,如果提示命令不存在,证明还没有安装,以CentOS为例,安装命令 ...

  8. VMware安装64位操作系统提示Intel VT-x处于禁用状态的解决办法

    用VMware安装64位操作系统时,如果目前本地的操作系统是64位的,那么可以说明CPU是肯定支持64位的,这时候如果提示此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态.这个 ...

  9. Java for LeetCode 041 First Missing Positive

    Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] ...

  10. July 11th, Week 29th Monday, 2016

    I want to win a trophy, it's the most important. 我希望获得冠军奖杯,这是最重要的事情. Win a trophy, stand on the very ...