如何对GridView行自动编号?
时间:2010-11-03 来源:与时俱进
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
Label olabel;
if (e.Row.RowType == DataControlRowType.DataRow)
{
olabel = (Label)e.Row.Cells[0].FindControl("lblNo");
olabel.Text = Convert.ToString(e.Row.RowIndex + 1);
}
}
如果遇到有分页时,以上代码对每一页都是从1开始编号,所以对于有分页的情况需要修改成:
{
Label olabel;
if (e.Row.RowType == DataControlRowType.DataRow)
{
olabel = (Label)e.Row.Cells[0].FindControl("lblNo");
olabel.Text = Convert.ToString(e.Row.RowIndex + 1);
}
}
如果遇到有分页时,以上代码对每一页都是从1开始编号,所以对于有分页的情况需要修改成:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
Label olabel;
if (e.Row.RowType == DataControlRowType.DataRow)
{
olabel = (Label)e.Row.Cells[0].FindControl("lblNo");
olabel.Text = Convert.ToString(GridView1.PageIndex * GridView1.PageSize + e.Row.RowIndex + 1);
}
}
{
Label olabel;
if (e.Row.RowType == DataControlRowType.DataRow)
{
olabel = (Label)e.Row.Cells[0].FindControl("lblNo");
olabel.Text = Convert.ToString(GridView1.PageIndex * GridView1.PageSize + e.Row.RowIndex + 1);
}
}
相关阅读 更多 +