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

 

你可能感兴趣的:(ASP.NET DridView 显示行号)