The first way, directly in the ASPX page of the GridView Template column. The disadvantage is that the second page of the page is re-started.
<asp:templatefield headertext= "serial number" insertvisible= "False" >
<itemstyle horizontalalign= "Center"/>
<ItemTemplate>
<% #Container .dataitemindex+1%>
</ItemTemplate>
</asp:TemplateField>
The second method is calculated when paging, which accumulates downward.
<asp:templatefield headertext= "serial number" insertvisible= "False" >
<itemstyle horizontalalign= "Center"/>
<ItemTemplate>
<asp:label id= "Label2" runat= "Server" text= "<%# this. Mylistgridview.pageindex * this. Mylistgridview.pagesize + this. MyListGridView.Rows.Count + 1%> "/>
</ItemTemplate>
</asp:TemplateField>
There is another way to put it in CS code, similar to the second.
<asp:boundfield headertext= "serial number" >
<itemstyle horizontalalign= "Center"/>
</asp:BoundField>
protected void Mygridview_rowdatabound (object sender, GridViewRowEventArgs e)
{
if (e.row.rowindex! =-1)
{
int INDEXID = This.myGridView.PageIndex * this.myGridView.PageSize + e.row.rowindex + 1;
E.row.cells[0]. Text = Indexid.tostring ();
}
}
Repeater itself with this property to get the current line number, without the programmer binding the line number, Container.itemindex can be obtained, see the following example:
<asp:repeater id= "Repeater1" runat= "Server" >
<ItemTemplate>
Line number: <% #Container. ItemIndex%>
</ItemTemplate>
</asp:Repeater>
If the above example, Repeater has already bound the data, and the data is at least a record, then the line number will be displayed, line number from zero, if you want to change from 1 onwards, you can change the above code to Container.itemindex + 1, see the following example:
<asp:repeater id= "Repeater1" runat= "Server" >
<ItemTemplate>
Line number: <% #Container. ItemIndex + 1%>
</ItemTemplate>
</asp:Repeater>
You can do it.
<asp:repeater id= "Repeater1" runat= "Server" >
<ItemTemplate>
<%# Container.itemindex + 1%>
<%# (Container as RepeaterItem). ItemIndex + 1%>
</ItemTemplate>
</asp:Repeater>
Aspxgridview,repeater adding an automatic sequence number column