当前位置: 代码迷 >> Web前端 >> GridView控件自动编号兑现
  详细解决方案

GridView控件自动编号兑现

热度:8   发布时间:2012-08-15 16:57:17.0
GridView控件自动编号实现

GridView控件自动编号实现,主要有二种方法

一、
   <asp:TemplateField HeaderText="序号" ItemStyle-Width="50px" HeaderStyle-Width="50px">
                                        <ItemStyle HorizontalAlign="Center" Wrap="false" />
                                        <ItemTemplate>
                                            <%#Container.DataItemIndex+1%>
                                        </ItemTemplate>
                                    </asp:TemplateField>

二、

  protected void gvDataTypeManage_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //自动编号实现
            if (e.Row.RowIndex != -1)
            {
                int id = e.Row.RowIndex + 1;
                e.Row.Cells[0].Text = id.ToString();
            }
        }

注意:当有分页控件一起使用时,一分页后,编号又是从0开始了,这时候,我们可以加上 当前页索引*页面大小。其中ExtendPager1是分页控件

protected void gvMethodInfoManage_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //自动编号实现
            if (e.Row.RowIndex != -1)
            {
                int id = e.Row.RowIndex + ExtendPager1.PageSize * (ExtendPager1.PageIndex - 1) + 1;
                e.Row.Cells[0].Text = id.ToString();
            }
        }

  相关解决方案