The first method is directly in the GridView template column on the Aspx page. The disadvantage is that it starts again when the second page is displayed.
Copy codeThe Code is as follows:
<Asp: TemplateField HeaderText = "no." InsertVisible = "False">
<ItemTemplate>
<% # Container. DataItemIndex + 1%>
</ItemTemplate>
</Asp: TemplateField>
In the second method, the computation is performed during paging, so that the computation is accumulated and added down.
Copy codeThe Code is as follows:
<Asp: TemplateField HeaderText = "no." InsertVisible = "False">
<ItemStyle HorizontalAlign = "Center"/>
<HeaderStyle HorizontalAlign = "Center"/>
<ItemTemplate>
<Asp: Label ID = "Label2" runat = "server" Text = '<% # this. GridView1.PageIndex * this. GridView1.PageSize + this. GridView1.Rows. Count + 1%>'/>
</ItemTemplate>
</Asp: TemplateField>
Another method is put in the cs code, which is similar to the second one.
Copy codeThe Code is as follows:
<Asp: BoundField HeaderText = "no."> </asp: BoundField>
Protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)
{
If (e. Row. RowIndex! =-1)
{
Int indexID = this. GridView1.PageIndex * this. myGridView. PageSize + e. Row. RowIndex + 1;
E. Row. Cells [0]. Text = indexID. ToString ();
}
}