Add automatic numbers to repeater, datalist, and DataGrid

Source: Internet
Author: User

Add automatic numbers to repeater, datalist, and DataGrid
Column number content
1 Taye
2 box
3 glass
4 Starcraft

I. Forward order
A. If allowpaging is set to false, use the following methods:
1 <asp: DataGrid id = "datagrid1" runat = "server">
2 <columns>
3 <asp: templatecolumn>
4 <itemtemplate>
5 <% # container. itemindex + 1%>
6 </itemtemplate>
7 </ASP: templatecolumn>
8 </columns>
9 </ASP: DataGrid>

But the more interesting method is to use this method:
1 <asp: DataGrid id = "datagrid1" runat = "server">
2 <columns>
3 <asp: templatecolumn>
4 <itemtemplate>
5 <% # This. datagrid1.items. Count + 1%>
6 </itemtemplate>
7 </ASP: templatecolumn>
8 </columns>
9 </ASP: DataGrid>

Some may wonder why items. Count is like this, rather than getting all in total, but it is easy to understand if you understand the binding process. [From the above point of view, it is bound in the itemcreated event, so the resulting items. Count is the current serial number.]

B. Under allowpaging = "true", if the DataGrid supports pagination, it can be as follows:
1 <asp: DataGrid id = "datagrid1" runat = "server" allowpaging = "true">
2 <columns>
3 <asp: templatecolumn>
4 <itemtemplate>
5 <% # This. datagrid1.currentpageindex * This. datagrid1.pagesize + container. itemindex + 1%>
6 </itemtemplate>
7 </ASP: templatecolumn>
8 </columns>
9 </ASP: DataGrid>

Ii. Reverse Order Method
Serial number content
4 Taye
3 box
2 GLASS
1 Starcraft

As you can see above, use this. datagrid1.items. count-container. the itemindex + 1 method cannot be implemented. The obtained value and the Plenary value are 1, which is the same for paging. so we need to get the number of rows of the data source at the beginning:
1 private int rowscount = 0;
2 protected int rowscount
3 {
4 get {return rowscount ;}
5 set {This. rowscount = value ;}
6}
7
8 private void page_load (Object sender, system. eventargs E)
9 {
10 // place the user here Code To initialize the page
11 if (! Ispostback)
12 This. binddata ();
13}
14 private void binddata ()
15 {
16 sqlconnection Cn = new sqlconnection ("Server = (local); database = northwind; uid = sa; Pwd = ");
17 string STR = @ "select employees. employeeid, orders. employeeid
18 from employees inner join
19 orders on employees. employeeid = orders. employeeid ";
20 sqldataadapter sqlda = new sqldataadapter (STR, CN );
21 dataset DS = new dataset ();
22 sqlda. Fill (DS );
23 This. rowscount = Ds. Tables [0]. Rows. count;
24 This. datagrid1.datasource = Ds;
25 this. datagrid1.databind ();
26}

1 <asp: DataGrid id = "datagrid1" runat = "server" allowpaging = "true">
2 <columns>
3 <asp: templatecolumn>
4 <itemtemplate>
5 <% # rowscount-maid. currentpageindex * maid %>
6 </itemtemplate>
7 </ASP: templatecolumn>
8 </columns>
9 </ASP: DataGrid>

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.