代码改变世界

SPGridView的使用--增加自动生成的序列号

2010-06-13 09:26  Leason Li  阅读(468)  评论(0编辑  收藏  举报

  我们在用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();            
    }        
}