如何对GridView行自动编号?
2010-11-25 17:31
239 查看
如何对GridView行自动编号?
有时候会遇到这样的情况,就是需要对GridView表格显示的结果增加一列自动递增编号列,以标示每一行的序号。要实现这一功能,首先在 GridView 第一列加入一个 TemplateField,并在 TemplateField 的 ItemTemplate 加入一个 Label (ID=lblNo),*.aspx 对应代码如下:<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
DataKeyNames="Flag,ID" DataSourceID="SqlDataSource1" EmptyDataText="无记录。">
<Columns>
<asp:TemplateField HeaderText="编号">
<ItemTemplate>
<asp:Label ID="lblNo" runat="server" Text="Label"></asp:Label>
</ItemTemplate>
<ItemStyle Wrap="True" />
<HeaderStyle Wrap="False" />
</asp:TemplateField>
</Columns>
</asp:GridView>
然后在 GridView 的 RowDataBound 事件中,设定每一列的 lblNo 的 Text 属性值为 RowIndex + 1(因为 RowIndex 起始编号为 0 ,所以每一行的自动编号为 RowIndex + 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(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);
}
}
相关文章推荐
- 如何对GridView行自动编号?
- 如何对GridView行自动编号?
- 【Vegas原创】GridView前台绑定HyperLink参数&自动编号
- 如何设置WORD中图片自动编号
- 如何在ASPXGridView绑定数据不满一页时自动添加空行
- Altium Designer如何设置元件自动编号解决编号冲突
- access如何用代码在“默认值”里实现自动编号?单据流水号生成问题。
- mysql insert语句后如何获取insert数据的主键值自动编号
- 手把手教你如何扩展GridView之自动排序篇
- Word中如何自动编号公式并可以交叉引用(利用表格、文档部件、题注)
- GridView加自动编号列
- gridview自动编号
- GridView 下增加自动编号列 及鼠标滑过行变色
- GridView支持分页的自动编号
- CCBPM(CCFlow与JFlow)中如何让表单字段自动生成编号
- GridView控件自动编号实现
- mysql如何把查询到的数据加上一个自动编号字段
- 【Vegas原创】GridView前台绑定HyperLink参数&自动编号
- 如何在ASPXGridView绑定数据不满一页时自动添加空行
- 如何自动生成定单编号?