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();            
        }        
    }
  • 相关阅读:
    C语言博客作业03--函数
    C博客作业02--循环结构
    C博客作业01--分支、顺序结构
    我的第一篇博客
    迭代购物车Dao&&GUI
    Java购物车大作业01
    DS-查找
    DS-图
    DS--树
    DS博客作业02--栈和队列
  • 原文地址:https://www.cnblogs.com/Bany/p/1828341.html
Copyright © 2011-2022 走看看