Auto serial number is added in the gridview control. There are multiple implementation methods, you only need to determine according to the practical requirements. Generally, it is divided into the background writing method and the foreground writing method. The background writing method is usually used without paging. The principle is to process the rowdatabound event when the gridview binds data.
The column on the page:
< ASP: boundfield Headertext = "No" />
Or use
< ASP: templatefield Headertext = "No" >
< Itemtemplate >
</ Itemtemplate >
</ ASP: templatefield >
CSCodeIs:
Protected void gridview1_rowdatabond (Object sender, gridviewroweventargs E)
{
If (E. Row. rowindex> = 0)
{
E. Row. cells [0]. Text = convert. tostring (E. Row. rowindex + 1 );
}
}
The page is directly implemented, for example, intuitively. You can understand the meaning of the container. dataitemindex attribute:
< ASP: templatefield headertext = " Serial number " >
< Itemtemplate >
<% # Container. dataitemindex + 1 %>
</ Itemtemplate >
</ ASP: templatefield >
In the case of paging, the paging method in ASP. NET usually uses the pagination tool provided by the gridview and the aspnetpager. The paging method of the gridview is as follows:
< ASP: templatefield headertext = " Serial number " >
< Itemtemplate >
<% # This . Gridview1.pageindex * This . Gridview1.pagesize
+Gridview1.rows. Count+ 1%>
</Itemtemplate>
</ASP: templatefield>
In the case of aspnetpager paging:
< ASP: templatefield headertext = " Serial number " >
< Itemtemplate >
<% #( This . Pager1.currentpageindex - 1 ) * This . pager1.pagesize
+ container. dataitemindex + 1 %>
itemtemplate >
Asp: templatefield >
http://www.cnblogs.com/aijun/archive/2011/03/15/1984563.html