asp.net中GridView排序的手动实现
2015-01-06 17:52
399 查看
GridView自带了数据排序功能。在设计视图下,只能对GridView的排序数据列和排序方向进行静态设置。如果要自己实现排序功能,在后台程序中,则需要用Attributes方式对GridView的这两个属性进行动态设置。
示例如下:
(前台)
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_1.gif)
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_2.gif)
<%
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_3.gif)
@
Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
<!DOCTYPE
html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
<html
xmlns="http://www.w3.org/1999/xhtml" >
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
<head
runat="server">
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
<title>无标题页</title>
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
</head>
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
<body>
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
<form
id="form1" runat="server">
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
<div>
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
<asp:GridView
ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" AllowSorting="True" OnSorting="GridView1_Sorting">
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
<FooterStyle
BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
<RowStyle
BackColor="#EFF3FB" />
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
<Columns>
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
<asp:BoundField
DataField="id" HeaderText="ID" SortExpression="id" />
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
<asp:BoundField
DataField="name" HeaderText="NAME" SortExpression="name" />
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
<asp:BoundField
DataField="age" HeaderText="AGE" SortExpression="age" />
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
</Columns>
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
<PagerStyle
BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_<br/>4000<br/>9078867_4.gif)
<SelectedRowStyle
BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
<HeaderStyle
BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
<EditRowStyle
BackColor="#2461BF" />
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
<AlternatingRowStyle
BackColor="White" />
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
</asp:GridView>
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
</div>
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
</form>
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
</body>
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
</html>
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
前台注意点:
需要对GridView启用AllowSorting、设置OnSorting事件,对需要排序的列设定SortExpression属性。
(后台)
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// 设定初始排序参数值
// 错误的属性设置方法:SortExpression、SortDirection均是GridView只读属性,无法直接赋值。
//this.GridView1.SortExpression = "id";
//this.GridView1.SortDirection = "ASC";
// 正确的属性设置方法
this.GridView1.Attributes.Add("SortExpression", "id");
this.GridView1.Attributes.Add("SortDirection", "ASC");
// 绑定数据源到GridView
this.BindGridView();
}
}
/// <summary>
/// GridView排序事件
/// </summary>
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
// 从事件参数获取排序数据列
string sortExpression = e.SortExpression.ToString();
// 假定为排序方向为“顺序”
string sortDirection = "ASC";
// “ASC”与事件参数获取到的排序方向进行比较,进行GridView排序方向参数的修改
if (sortExpression == this.GridView1.Attributes["SortExpression"])
{
//获得下一次的排序状态
sortDirection = (this.GridView1.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
}
// 重新设定GridView排序数据列及排序方向
this.GridView1.Attributes["SortExpression"] = sortExpression;
this.GridView1.Attributes["SortDirection"] = sortDirection;
this.BindGridView();
}
/// <summary>
/// 绑定到GridView
/// </summary>
private void BindGridView()
{
// 获取GridView排序数据列及排序方向
string sortExpression = this.GridView1.Attributes["SortExpression"];
string sortDirection = this.GridView1.Attributes["SortDirection"];
// 调用业务数据获取方法
DataTable dtBind = this.getDB();
// 根据GridView排序数据列及排序方向设置显示的默认数据视图
if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
{
dtBind.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
}
// GridView绑定并显示数据
this.GridView1.DataSource = dtBind;
this.GridView1.DataBind();
}
/// <summary>
/// 获取数据源的方法
/// </summary>
/// <returns>数据源</returns>
private DataTable getDB()
{
DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("name");
dt.Columns.Add("age");
dt.Rows.Add(new object[] { "000001", "hekui", "26" });
dt.Rows.Add(new object[] { "000002", "zhangyu", "26" });
dt.Rows.Add(new object[] { "000003", "zhukundian", "27" });
dt.Rows.Add(new object[] { "000004", "liyang", "25" });
dt.Rows.Add(new object[] { "000005", "caili", "27" });
return dt;
}
}
原文转载自http://www.360doc.com/content/15/0106/17/21392682_438677308.shtml
示例如下:
(前台)
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_1.gif)
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_2.gif)
<%
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_3.gif)
@
Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
<!DOCTYPE
html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
<html
xmlns="http://www.w3.org/1999/xhtml" >
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
<head
runat="server">
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
<title>无标题页</title>
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
</head>
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
<body>
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
<form
id="form1" runat="server">
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
<div>
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
<asp:GridView
ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" AllowSorting="True" OnSorting="GridView1_Sorting">
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
<FooterStyle
BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
<RowStyle
BackColor="#EFF3FB" />
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
<Columns>
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
<asp:BoundField
DataField="id" HeaderText="ID" SortExpression="id" />
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
<asp:BoundField
DataField="name" HeaderText="NAME" SortExpression="name" />
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
<asp:BoundField
DataField="age" HeaderText="AGE" SortExpression="age" />
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
</Columns>
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
<PagerStyle
BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_<br/>4000<br/>9078867_4.gif)
<SelectedRowStyle
BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
<HeaderStyle
BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
<EditRowStyle
BackColor="#2461BF" />
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
<AlternatingRowStyle
BackColor="White" />
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
</asp:GridView>
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
</div>
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
</form>
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
</body>
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
</html>
![](http://image5.360doc.com/DownloadImg/2009/11/15/466494_9078867_4.gif)
前台注意点:
需要对GridView启用AllowSorting、设置OnSorting事件,对需要排序的列设定SortExpression属性。
(后台)
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// 设定初始排序参数值
// 错误的属性设置方法:SortExpression、SortDirection均是GridView只读属性,无法直接赋值。
//this.GridView1.SortExpression = "id";
//this.GridView1.SortDirection = "ASC";
// 正确的属性设置方法
this.GridView1.Attributes.Add("SortExpression", "id");
this.GridView1.Attributes.Add("SortDirection", "ASC");
// 绑定数据源到GridView
this.BindGridView();
}
}
/// <summary>
/// GridView排序事件
/// </summary>
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
// 从事件参数获取排序数据列
string sortExpression = e.SortExpression.ToString();
// 假定为排序方向为“顺序”
string sortDirection = "ASC";
// “ASC”与事件参数获取到的排序方向进行比较,进行GridView排序方向参数的修改
if (sortExpression == this.GridView1.Attributes["SortExpression"])
{
//获得下一次的排序状态
sortDirection = (this.GridView1.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
}
// 重新设定GridView排序数据列及排序方向
this.GridView1.Attributes["SortExpression"] = sortExpression;
this.GridView1.Attributes["SortDirection"] = sortDirection;
this.BindGridView();
}
/// <summary>
/// 绑定到GridView
/// </summary>
private void BindGridView()
{
// 获取GridView排序数据列及排序方向
string sortExpression = this.GridView1.Attributes["SortExpression"];
string sortDirection = this.GridView1.Attributes["SortDirection"];
// 调用业务数据获取方法
DataTable dtBind = this.getDB();
// 根据GridView排序数据列及排序方向设置显示的默认数据视图
if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
{
dtBind.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
}
// GridView绑定并显示数据
this.GridView1.DataSource = dtBind;
this.GridView1.DataBind();
}
/// <summary>
/// 获取数据源的方法
/// </summary>
/// <returns>数据源</returns>
private DataTable getDB()
{
DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("name");
dt.Columns.Add("age");
dt.Rows.Add(new object[] { "000001", "hekui", "26" });
dt.Rows.Add(new object[] { "000002", "zhangyu", "26" });
dt.Rows.Add(new object[] { "000003", "zhukundian", "27" });
dt.Rows.Add(new object[] { "000004", "liyang", "25" });
dt.Rows.Add(new object[] { "000005", "caili", "27" });
return dt;
}
}
原文转载自http://www.360doc.com/content/15/0106/17/21392682_438677308.shtml
相关文章推荐
- Asp.net中,点击GridView表头实现数据的排序
- asp.net GridView排序简单实现
- (分页、排序等)asp.net 2.0下一个标准GRIDVIEW功能的实现
- Asp.net中,点击GridView表头实现数据的排序
- asp.net GridView排序简单实现
- [导入]ASP.NET 2.0中GridView无限层复杂表头的实现
- (ASP.NET)用动态属性和DataView实现DataGrid的双向排序
- ASP.NET 2.0中GridView无限层复杂表头的实现
- ASP.NET 2.0中GridView无限层复杂表头的实现
- ASP.NET 2.0中GridView无限层复杂表头的实现
- Asp.net 2.0 GridView的几个事件(如实现: 行的双击/单击/捕捉键盘按键/鼠标悬浮/移出效果)(示例代码下载)
- 用编码实现gridview的创建!(一)(转www.asp.net)
- ASP.NET 2.0中GridView无限层复杂表头的实现
- ASP.NET 2.0中GridView无限层复杂表头的实现
- [导入]ASP.NET 2.0中GridView无限层复杂表头的实现
- asp.net 2.0下一个标准GRIDVIEW功能的实现(不用datasource控件)
- asp.net 2.0下一个标准GRIDVIEW功能的实现(不用datasource控件)
- asp.net 2.0下一个标准GRIDVIEW功能的实现(不用datasource控件)
- asp.net 2.0下一个标准GRIDVIEW功能的实现(不用datasource控件)