您的位置:首页 > 其它

GridView控件自动编号实现

2012-06-29 15:33 323 查看
GridView控件自动编号实现,主要有二种方法

一、

<asp:TemplateField HeaderText="序号" ItemStyle-Width="50px" HeaderStyle-Width="50px">

<ItemStyle HorizontalAlign="Center" Wrap="false" />

<ItemTemplate>

<%#Container.DataItemIndex+1%>

</ItemTemplate>

</asp:TemplateField>

二、

protected void gvDataTypeManage_RowDataBound(object sender, GridViewRowEventArgs e)

{

//自动编号实现

if (e.Row.RowIndex != -1)

{

int id = e.Row.RowIndex + 1;

e.Row.Cells[0].Text = id.ToString();

}

}

注意:当有分页控件一起使用时,一分页后,编号又是从0开始了,这时候,我们可以加上 当前页索引*页面大小。其中ExtendPager1是分页控件

protected void gvMethodInfoManage_RowDataBound(object sender, GridViewRowEventArgs e)

{

//自动编号实现

if (e.Row.RowIndex != -1)

{

int id = e.Row.RowIndex + ExtendPager1.PageSize * (ExtendPager1.PageIndex - 1) + 1;

e.Row.Cells[0].Text = id.ToString();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: