Use of spgridview-Add the automatically generated serial number

Source: Internet
Author: User

When we use the gridview and spgridview for data presentation and business processing, we will inevitably need to add the "Serial Number" column. We will first introduce two different implementation methods:

When no page is displayed:

1: when the field is manually added on the. ASPX page, you can use the following method:

Code

<Asp: templatefield headertext = "& lt; nobr & gt; No. & lt;/nobr & gt;">
<Itemtemplate>
<Asp: Label runat = "server" id = "lblno" text = '<% # container. dataitemindex + 1%>'> </ASP: Label>
</Itemtemplate>
</ASP: templatefield>

2: when the field is generated on the background. CS page, use the following method:

Code

// Add the auto-generated sequence number
Protected void spgridviewdomainrowdatabound (Object sender, gridviewroweventargs E)
{
If (E. Row. rowindex! =-1)
{
Int indexid = E. Row. rowindex + 1;
E. Row. cells [0]. Text = indexid. tostring ();
}
}

When paging:

1: when the field is manually added on the. ASPX page, you can use the following method:

Code

<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>

2: when the field is generated on the background. CS page, use the following method:

Code

<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 ();
}
}

 

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.