Aspxgridview,repeater adding an automatic sequence number column

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.