Add auto serial number columns in gridview and repeater

Source: Internet
Author: User

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.

<Asp: templatefield headertext = "no." insertvisible = "false">
<Itemstyle horizontalalign = "center"/>
<Headerstyle horizontalalign = "center" width = "5%"/>
<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.







Another method is put in CSCodeIs similar to the second type.

<Asp: boundfield headertext = "no.">
<Itemstyle horizontalalign = "center"/>
<Headerstyle horizontalalign = "center" width = "5%"/>
</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 ();
}
}

The repeater itself has the property to get the current row number,ProgramThe member binds this row number and container. itemindex can be obtained. See the following example:

<Asp: repeater id = "repeater1" runat = "server">
<Itemtemplate>
Row number: <% #Container.Itemindex %>
</Itemtemplate>
</ASP: repeater>

If in the preceding example, the repeater has already bound data and the data is at least one record, the row number is displayed, starting from scratch. If you want to change it to start from 1, you can change the above Code to container. itemindex+ 1See the following example:

<Asp: repeater id = "repeater1" runat = "server">
<Itemtemplate>
Row number: <% #Container. itemindex+ 1 %>
</Itemtemplate>
</ASP: repeater>

You can.

< ASP: Repeater ID = "Repeater1" Runat = "Server" >
< Itemtemplate >
<% # Container. itemindex + 1 %>
<% # (Container As Repeateritem). itemindex + 1 %>
</ Itemtemplate >
</ ASP: Repeater >

Reprinted from: http://blog.163.com/wwwoop2003@126/blog/static/42004852200891654142842/

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.