GridView 的排序和分页

Gridview 带了排序功能,如果在Web页面上拉一个GridView,并在页面上指定了 DatasourceID

(从控件的智能菜单设置sql 数据源),且GridView启用AllowSorting、需要排序的列设定SortExpression属性。

运行网站,字段将可以自动排序。

设置pagesize 之后,gridview 也会自动具有分页功能。

如果使用后台的方法设置 gridview的数据源 (DataSource),需要自己写 GridView1_Sorting事件。

同样分页也需要自己写  PageIndexChanging事件。

不然会出现 :激发了未处理的事件  “PageIndexChanging激发了未处理的事件 “Sorting”  类似错误

下面转载一个有关 排序的例子

注: page_load 事件可以预设排序规则,此部分可以省略。

       getDB 方法是从数据库中获取需要的gridview 的数据源,根据业务情况编写,此处省略

       开始我在使用下面的方法是,获取的数据源(如,getDB 方法返回)

       中使用的 sql 查询语句里 使用了order by,返回的dataset 将是已经按照某字段排序过的,

      再使用 下面的方法排序将不准确,

 

 
  
1 protected void Page_Load( object sender, EventArgs e)
2 {
3 if ( ! IsPostBack)
4 {
5 // 设定初始排序参数值
6
7 // 错误的属性设置方法:SortExpression、SortDirection均是GridView只读属性,无法直接赋值。
8 // this.GridView1.SortExpression = "id";
9 // this.GridView1.SortDirection = "ASC";
10
11 // 正确的属性设置方法
12   this .GridView1.Attributes.Add( " SortExpression " , " id " );
13 this .GridView1.Attributes.Add( " SortDirection " , " ASC " );
14
15 // 绑定数据源到GridView
16   this .BindGridView();
17 }
18 }
19
20 /// <summary>
21 /// GridView排序事件
22 /// </summary>
23   protected void GridView1_Sorting( object sender, GridViewSortEventArgs e)
24 {
25 // 从事件参数获取排序数据列
26   string sortExpression = e.SortExpression.ToString();
27
28 // 假定为排序方向为“顺序”
29   string sortDirection = " ASC " ;
30
31 // “ASC”与事件参数获取到的排序方向进行比较,进行GridView排序方向参数的修改
32 if (sortExpression == this .GridView1.Attributes[ " SortExpression " ])
33 {
34 // 获得下一次的排序状态
35 sortDirection = ( this .GridView1.Attributes[ " SortDirection " ].ToString() == sortDirection ? " DESC " : " ASC " );
36 }
37
38 // 重新设定GridView排序数据列及排序方向
39 this .GridView1.Attributes[ " SortExpression " ] = sortExpression;
40 this .GridView1.Attributes[ " SortDirection " ] = sortDirection;
41
42 this .BindGridView();
43 }
44
45 /// <summary>
46 /// 绑定到GridView
47 /// </summary>
48 private void BindGridView()
49 {
50 // 获取GridView排序数据列及排序方向
51 string sortExpression = this .GridView1.Attributes[ " SortExpression " ];
52 string sortDirection = this .GridView1.Attributes[ " SortDirection " ];
53
54 // 调用业务数据获取方法
55 DataTable dtBind = this .getDB();
56
57 // 根据GridView排序数据列及排序方向设置显示的默认数据视图
58 if (( ! string .IsNullOrEmpty(sortExpression)) && ( ! string .IsNullOrEmpty(sortDirection)))
59 {
60 dtBind.DefaultView.Sort = string .Format( " {0} {1} " , sortExpression, sortDirection);
61 }
62
63 // GridView绑定并显示数据
64 this .GridView1.DataSource = dtBind;
65 this .GridView1.DataBind();
66 }
67

 

 

分页方法:

 

 
  
1 protected void GridView1_PageIndexChanging( object sender, GridViewPageEventArgs e)
2 {
3 GridView1.PageIndex = e.NewPageIndex;
4 BindGridView(); // 自定义的数据绑定方法,重新绑定数据源
5
6 }

 

 

下面转一个分页模板:

 

 
  
1
2 < PagerTemplate >
3 < asp:LinkButton ID = " LinkButton1 " runat = " server " CommandName = " Page " CommandArgument = " First " Enabled = " <%# ((GridView)Container.Parent.Parent).PageIndex>0 %> " > 首页 </ asp:LinkButton >
4 < asp:LinkButton ID = " LinkButton4 " runat = " server " CommandName = " Page " CommandArgument = " Prev " Enabled = " <%# ((GridView)Container.Parent.Parent).PageIndex>0 %> " > 上一页 </ asp:LinkButton >
5 < asp:LinkButton ID = " LinkButton2 " runat = " server " CommandName = " Page " CommandArgument = " Next " Enabled = " <%# ((GridView)Container.Parent.Parent).PageIndex<((GridView)Container.Parent.Parent).PageCount-1 %> " > 下一页 </ asp:LinkButton >
6 < asp:LinkButton ID = " LinkButton3 " runat = " server " CommandName = " Page " CommandArgument = " Last " Enabled = " <%# ((GridView)Container.Parent.Parent).PageIndex<((GridView)Container.Parent.Parent).PageCount-1 %> " > 尾页 </ asp:LinkButton >
7 & nbsp; < span style = " font-family: Georgia; " > < asp:Label ID = " Label2 " runat = " server "
8 Text = ' <%#((GridView)Container.Parent.Parent).PageIndex+1 %> ' ></ asp:Label > / < asp:Label
9 ID = " Label3 " runat = " server " Text = ' <%#((GridView)Container.Parent.Parent).PageCount %> ' ></ asp:Label > & nbsp;每页 < asp:Label
10 ID = " Label4 " runat = " server " Text = ' <%# ((GridView)Container.Parent.Parent).PageSize%> ' ></ asp:Label > 条记录 & nbsp;全部
11 < asp:Label ID = " Label1 " runat = " server " Text = ' <%# ((System.Data.DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty)).Count %> ' ></ asp:Label > 条记录 </ span >
12 </ PagerTemplate >

上面的代码中需要页面已有 SqlDataSource1

 

如果是在后台代码里 指定数据源,可以使用一个后台方法来取得gridview 数据源的行数。

在此用类似这样的方法 <%# getGridViewRecordsCount() %> 进行绑定。

转载于:https://www.cnblogs.com/wtao/archive/2010/12/09/1900939.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值