SoFunction
Updated on 2025-03-07

GridView automatically adds serial number (three implementation methods)

The first method is directly in the Aspx page GridView template column. The disadvantage of this is that it starts again when paging the second page.
Copy the codeThe code is as follows:

<asp:TemplateField HeaderText="Serial Number" InsertVisible="False">
<ItemTemplate>
<%#+1%>
</ItemTemplate>
</asp:TemplateField>

The second method is calculated during paging, so that the total will be added downward.
Copy the codeThe code is as follows:

<asp:TemplateField HeaderText="Serial Number" InsertVisible="False">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center"/>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# this. * this. + this. + 1%>' />
</ItemTemplate>
</asp:TemplateField>

There is another way to put it in the cs code, which is similar to the second one.
Copy the codeThe code is as follows:

<asp:BoundField HeaderText="Serial Number" ></asp:BoundField>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if ( != -1)
{
int indexID = this. * + + 1;
[0].Text = ();
}
}