登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

樱之花

叶散的时候,你明白欢聚;花谢的时候,你明白青春.

 
 
 

日志

 
 
关于我

分类中“我的实验室”是我在日常工作中的一些知识总结,有些写的比较匆忙,可能大家在阅读时会产生困扰,后期有时间我会重新整理编辑,谢谢大家的到访,您们的支持是我前进的动力!

asp:GridView序号问题  

2009-12-11 17:09:45|  分类: .NET/C# |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

GridView控件中加自动序号,有多种实现方法,你只需要根据的实用要求来确定。总的来分为后台写法和前台写法,后台写法一般不考虑分页的情况下使用,原理就是在GridView 绑定数据时,在RowDataBound 事件中来处理。

页面的列为:

<asp:BoundField  HeaderText="序号" />

或用

<asp:TemplateField HeaderText="序号">
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>

CS代码为:

protected void GridView1_RowDataBond(object sender, GridViewRowEventArgs e)
{
         if (e.Row.RowIndex >= 0)
         {
          e.Row.Cells[0].Text = Convert.ToString(e.Row.RowIndex + 1);
         }          
   }

页面直接实现比如直观,知道Container.DataItemIndex 属性的含义就行:

<asp:TemplateField HeaderText="序号">
<ItemTemplate>
   
<%# Container.DataItemIndex + 1%>
 
</ItemTemplate>
</asp:TemplateField>

下面考虑的主要是分页情况下的,在ASP.NET中分页方法一般用GridView自带的分页工具和AspNetPager的比较多。GridView自带的分页写法:

<asp:TemplateField HeaderText="序号">
<ItemTemplate>
<%# this.GridView1.PageIndex  * this.GridView1.PageSize

         
+ GridView1.Rows.Count + 1%>
</ItemTemplate>
</asp:TemplateField>

AspNetPager分页情况下的写法为:

<asp:TemplateField HeaderText="序号">
<ItemTemplate>
   
<%# (this.Pager1.CurrentPageIndex - 1) * this.Pager1.PageSize

    
+ Container.DataItemIndex + 1%>
</ItemTemplate>
</asp:TemplateField>

  评论这张
 
阅读(913)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018