GridView的RowDataBind事件中实现记录行数的显示

来源:互联网 发布:博克软件 编辑:程序博客网 时间:2024/05/22 09:14

在asp.net2.0中使用GridView的模板列可以方便地实现GridView中添加行数的功能。
为了看到翻页后的行号,需要把GridView的AllowPaging属性设置为true
首先,在设计模式下为GridView的Columns添加一个模板列,把该模板列移动到第一列。
然后在GridView的RowDataBind事件中添加如下代码就可以实现记录行数的显示:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
   
    if (this.GridView1.PageIndex == 0)
    {
     
        if (e.Row.RowIndex != -1)
        {
          e.Row .Cells[0].Text = Convert.ToString(e.Row.RowIndex + 1);
        }
    }
    else
    {
        int start = this.GridView1.PageIndex * this.GridView1.PageSize;
        if (e.Row.RowIndex != -1)
        {
          e.Row .Cells[0].Text = Convert.ToString(e.Row.RowIndex + start + 1);
        }
    }
   

  }

 

如图

原创粉丝点击