GridView自动序号

 

2010-04-07 09:22 GridView自动序号如果是SQL2005可以这么操作

select ROW_NUMBER() OVER (ORDER BY CreateDate DESC) AS SortID,.....from TABLE.

为 Gridview 增加一个新的空白列,如下:
<asp:BoundField HeaderText="序号">
            <ItemStyle HorizontalAlign="Center" Width="26px" />
            </asp:BoundField>

在 GridView RowDataBound 事件中编写代码,如下:
protected void RowDataBond1(object sender, GridViewRowEventArgs e)
{
         if (e.Row.RowIndex > -1)
        {
         
         e.Row.Cells[0].Text = Convert.ToString(e.Row.RowIndex + 1);
        }
          
}
这样就会在运行时自动创一个顺序编号的字段。

也可以在前台直接写

此处是用GridView自带分页


<asp:TemplateField HeaderText="序号">
<ItemTemplate>
<%# this.GridView1.PageIndex * this.GridView1.PageSize + Container.DataItemIndex + 1%>
</ItemTemplate>
</asp:TemplateField>
    
      下面是用AspNetPager分页控件的情况:

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


第一种方式,直接在Aspx页面GridView模板列中.这种的缺点是到第二页分页时又重新开始了.

            <asp:TemplateField HeaderText="序号" InsertVisible="False">
              <ItemStyle HorizontalAlign="Center" />
              <HeaderStyle HorizontalAlign="Center" Width="5%" />
             <ItemTemplate>
              <%#Container.DataItemIndex+1%>
            </ItemTemplate>
            </asp:TemplateField>
第二种方式分页时进行了计算,这样会累计向下加.
            <asp:TemplateField HeaderText="序号" InsertVisible="False">
              <ItemStyle HorizontalAlign="Center" />
              <HeaderStyle HorizontalAlign="Center" Width="5%" />
             <ItemTemplate>
                 <asp:Label ID="Label2" runat="server" Text='<%# this.MyListGridView.PageIndex * this.MyListGridView.PageSize + this.MyListGridView.Rows.Count + 1%>'/>
            </ItemTemplate>
            </asp:TemplateField>
还有一种方式放在cs代码中,和第二种相似.
          <asp:BoundField HeaderText="序号" >
              <ItemStyle HorizontalAlign="Center" />
              <HeaderStyle HorizontalAlign="Center" Width="5%" />
          </asp:BoundField>
        protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex != -1)
            {
                int indexID = this.myGridView.PageIndex * this.myGridView.PageSize + e.Row.RowIndex + 1;
                 e.Row.Cells[0].Text = indexID.ToString();
             }
         }
都是非常简单的,其实原理都是一样的.实现同一种效果,方法很多,实际上读取数据的时候也可以实现.
为了不至于字数太多,给阅读带来不便,SQL的方法放在以下链接里面:其实都是非常简单的入门级,可是有的时候容易忘记.
第一种:使用identity函数增加临时表的方法
select id = identity(int,1,1),* into #tmp from table
select * from #tmp
drop table #tmp

在SQL2005中新增了ROW_NUMBER()函数,给我们带来了很多方便,使用方法如下:
SELECT id,ROW_NUMBER() OVER (order by id)as RowNumber FROM Table有一个方便,as后的别名可以在语句后面作为条件单独使用.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值