GridView生成序号

一个经常碰到的情况:GridView需要添加一个序号列,并且从1开始自动编号。而数据库中的ID往往是不连续的(会有记录被删除的情况),我们无法绑定现有字段作为编号。因此我们需要手动给GridView编号。

思路:在GridView每一行,既Row,被生成的时候,在序号单元格中输出该行的ID+1(第一行是从0开始编的,因此要+1)

实现:

首先,在ASPX页面中,需要手动给GridView添加一个BoundField。示例代码如下:

 

  
  
< asp:GridView ID ="GridView1" runat ="server" AutoGenerateColumns ="False" EnableModelValidation ="True"
OnRowCreated
="GridView1_RowCreated" >
< Columns >
< asp:BoundField HeaderText ="序号" >
< HeaderStyle Height ="24px" />
< ItemStyle Font-Size ="14px" Height ="24px" />
</ asp:BoundField >
< asp:BoundField DataField ="Sn" HeaderText ="卡号" />
< asp:BoundField HeaderText ="货物规格" />
< asp:BoundField HeaderText ="数量" />
< asp:BoundField DataField ="GhsId" HeaderText ="供货商ID" />
< asp:BoundField DataField ="Shr" HeaderText ="收货人姓名" />
< asp:BoundField DataField ="ShrContact" HeaderText ="收货人电话" />
< asp:BoundField DataField ="ShAddress" HeaderText ="送货地址" />
< asp:BoundField HeaderText ="物流单号" />
< asp:BoundField DataField ="Bz" HeaderText ="备注" />
</ Columns >
< RowStyle BorderColor ="Black" BorderStyle ="Solid" BorderWidth ="1px" />
</ asp:GridView >

 

后台代码这样写:

 

  
  
// Gridview自动生成序号
protected void GridView1_RowCreated( object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex > - 1 )
{
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 >


页面直接实现比如直观,知道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 >

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值