How to add auto-increment columns to the DataGrid

Source: Internet
Author: User

I think we all know how to add auto-incrementing columns to the database. We can bind this auto-incrementing column to the DataGrid so that you can easily know the row number, today, I will introduce a method to simply display the column from the growth without using a database. Some people may say that since the database supports us, why do we do this? I would like to have two reasons: 1. Not all tables have auto-incrementing columns. 2. When auto-incrementing columns are not automatically copied, the number will be disconnected, even if copying is possible. However, it must be noted that this method can only display the serial number of the current page, that is, if there is a page, it can only mark the serial number of the current page. If you want to implement the paging function, it is easier to use the database than this method, because if you still use this method, the status will be processed. I will not consider this method here, but in Article In the end, I will provide a database solution.
Well, the following is the question. First, we need to place a DataGrid in the page. We use the northwind database as an example:
The DataGrid of the HTML page is as follows:
<Asp: DataGrid id = "grdtest" runat = "server" Height = "228px" width = "262px" autogeneratecolumns = "false" allowpaging = "true">
<Columns>
<Asp: templatecolumn>
<Itemtemplate>
<! -- Here is the key -->
<Span>
<% # Container. itemindex + 1%> </span>
</Itemtemplate>
</ASP: templatecolumn>
<Asp: boundcolumn datafield = "categoryname"> </ASP: boundcolumn>
<Asp: boundcolumn datafield = "Description"> </ASP: boundcolumn>
</Columns>
</ASP: DataGrid>
Next we can write his background Code CS file. We can add the binding method in its page_load as follows:
Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
Strconnection = configurationsettings. receivettings ["sa"]. tostring ();
Myconnection = new sqlconnection (strconnection );
Sqldataadapter myadapter = new sqldataadapter ("select categoryname, description from categories", myconnection );
// For paging convenience, DS is a global variable.
Myadapter. Fill (DS );
This. grdtest. datasource = Ds. Tables [0]. defaultview;
This. grdtest. databind ();
}

From the process above, we can see that we are using the table categories, so that we can generate a column of Self-increasing columns, which starts from 1. What if we want a column starting from 0? We can <! -- Here is the key --> Replace the following <span> with <asp: Label id = lblrownumber runat = "server" text = '<% # databinder. eval (container, "itemindex", "{0}") %> '>.

If we want to implement the paging and display method, we will use the datatable method. First, we will convert all the columns in the DataGrid into bound columns (not required for demonstration ). As follows:
<Asp: table id = "tbdata" runat = "server" backcolor = "lightsteelblue" Height = "13px" width = "16px" font-names = "" font-name = "" font-size = "8pt" cellpadding = "1" cellspacing = "0" bordercolor = "black" borderwidth = "1" gridlines = "both"> </ASP: table> <br/>
<Asp: DataGrid id = "grdtest" runat = "server" Height = "228px" width = "262px" autogeneratecolumns = "false" pagesize = "2" allowpaging = "true">
<Columns>
<Asp: boundcolumn datafield = "rownumber" headertext = "rownumber"> </ASP: boundcolumn>
<Asp: boundcolumn datafield = "categoryname"> </ASP: boundcolumn>
<Asp: boundcolumn datafield = "Description"> </ASP: boundcolumn>
</Columns>
</ASP: DataGrid>
Add a function in the background:
Private datatable getrownumbertable (datatable DT ){
Datacolumn Col = new datacolumn ("rownumber", type. GetType ("system. int32 "));
DT. Columns. Add (COL );
For (INT I = 0; I <= DT. Rows. Count-1; I ++ ){
If (0 = I)
DT. Rows [I] [col] = 1;
Else
DT. Rows [I] [col] = convert. toint32 (Dt. Rows [I-1] [col]) + 1;
}
Return DT;
}
Then we changed the original data source to the following:
This. grdtest. datasource = This. getrownumbertable (Ds. Tables [0]). defaultview;
In this way, even if the pages are paginated, the numbers are consecutive and the numbers are applied to all rows rather than the rows on the current page.

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.