ASP.NET DridView 显示行号

方法一

aspx

GridView 添加 

OnDataBound="GridView1_DataBound"

<asp:TemplateField ItemStyle-Width="50px" ItemStyle-HorizontalAlign="Center">
                    <HeaderTemplate>
                        序号</HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
 aspx.cs

加入

protected void GridView1_DataBound(object sender, EventArgs e)
        {
            int num = GridView1.Rows.Count;
            for (int i = 0; i < num; i++)
            {
                Label lbl = (Label)GridView1.Rows[i].FindControl("Label1");
                lbl.Text = (i+1).ToString();
            }
        }

方法二(简单):

<asp:TemplateField ItemStyle-Width="50px" ItemStyle-HorizontalAlign="Center">
                    <HeaderTemplate>
                        序号</HeaderTemplate>
                    <ItemTemplate>
                        <%#Container.DataItemIndex+1 %></ItemTemplate>
 </asp:TemplateField>
 OK

 

posted on 2012-04-18 09:57  黄兆张's Blog  阅读(717)  评论(0编辑  收藏  举报

导航