zoukankan      html  css  js  c++  java
  • SPGridView的使用增加自动生成的序列号

      我们在用Gridview,SPGridview进行数据展现和业务处理的时候,难免会需要增加“序号”的栏位,先介绍两种不同的实现方式:

    当没有分页时:

    1:当栏位是在.ASPX页面手动添加的时候,可使用如下方法:

    代码
            <asp:TemplateField HeaderText="&lt;nobr&gt;序号&lt;/nobr&gt;">
                    
    <ItemTemplate>
                           
    <asp:Label runat="server" ID="lblNo" Text='<%# Container.DataItemIndex   +   1 %>'></asp:Label>
                    
    </ItemTemplate>
            
    </asp:TemplateField> 

    2:当栏位是在后台.CS页面自己生成的栏位的时候,使用如下方法:

    代码
        //添加自动生成的序号
        protected void SPGridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            
    if (e.Row.RowIndex != -1)
            {
                
    int indexID = e.Row.RowIndex + 1;
                e.Row.Cells[
    0].Text = indexID.ToString();
            } 
        }

    当分页时候:

    1: 当栏位是在.ASPX页面手动添加的时候,可使用如下方法:

    代码
    <asp:TemplateField HeaderText="序号" InsertVisible="False">
               
    <ItemStyle HorizontalAlign="Center" />
               
    <HeaderStyle HorizontalAlign="Center"/>
                
    <ItemTemplate>
                       
    <asp:Label ID="Label2" runat="server" Text='<%# this.GridView1.PageIndex * this.GridView1.PageSize + this.GridView1.Rows.Count + 1%>' />
               
    </ItemTemplate>
    </asp:TemplateField>

    2:当栏位是在后台.CS页面自己生成的栏位的时候,使用如下方法:

    代码
    <asp:BoundField HeaderText="序号" ></asp:BoundField>
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)        
    {            
        
    if (e.Row.RowIndex != -1)            
        {                
            
    int indexID = this.GridView1.PageIndex * this.myGridView.PageSize + e.Row.RowIndex + 1;                
            e.Row.Cells[
    0].Text = indexID.ToString();            
        }        
    }
  • 相关阅读:
    [NOI2012]美食节——费用流(带权二分图匹配)+动态加边
    mysqlsla slow-query常用用法
    [POI2000]病毒
    mysqlsla安装
    mysqldumpslow
    [学习笔记]约数
    查看MySQL数据的连接
    [学习笔记]质数
    关于ulimit -a中需要修改的两个值
    Miller-Rabin与二次探测
  • 原文地址:https://www.cnblogs.com/Bany/p/1828341.html
Copyright © 2011-2022 走看看