Next, we will discuss the "Add new row" function in the ASP. NET DataGrid Control.

Source: Internet
Author: User
I searched in the garden and found that no matter whether panchengyong's "Add a new row quickly in the DataGrid (C #)" or Piccolo's "Add a new row quickly in the DataGrid", it is just a simple implementation of adding a new line, there is still room for improvement in the function.

Add a new row in the DataGrid, Asp. net developer: Dino Esposito, his "build web solution-application ASP. net and ADO. NET provides methods similar to the two above, but Dino adds some features. The first is to add a new line. The linkbutton is placed in the footer, at the same time, this linkbutton is enabled only when the page is paged to the last page, because it is not advisable to add a new line to other pages. Second, if the added line causes pagination, switch to the new page and enter the editing mode of this line.

Some time ago, I also used this feature and found that there are still two problems:
1. Click "Add new line" to edit the new row. If you click "Add new line" again, the newly added line will become blank and the new line will be displayed in the editing mode.
2. When the page is turned over, the edititemindex of the DataGrid is not restored to-1, which causes other rows with the same index to enter the editing mode.

So I made a slight modification and solved these two problems. The final effect was as follows:
Figure 1: when the last page is not displayed, the Add New Line Button is unavailable.

Figure 2: the button is available on the last page

Figure 3: when the newly added row enters the editing mode, the button cannot be clicked again. After the button is canceled, the new row is deleted and restored to Figure 2.

Linkbutton In the footer template: < Footertemplate >
< ASP: linkbutton ID = Lbaddnewrow Runat = "Server" Commandname = "Addnewrow" Enabled = "<% # Isenableaddnewrow () %>" > Add new line </ ASP: linkbutton >
</ Footertemplate >

PageCodeIsenableaddnewrow method code in:

1 Protected   Bool Isenableaddnewrow ()
2 {
3 // The new line feature is enabled only when the last page is not in edit mode.
4 Return (Dgdata. currentpageindex = Dgdata. pagecount -   1 && Dgdata. edititemindex =   - 1 );
5 }

Itemcommand Event code in page code: 1 Private   Void Dgdata_itemcommand ( Object Source, system. Web. UI. webcontrols. datagridcommandeventargs E)
2 {
3 If (E. commandname =   " Addnewrow " )
4 {
5 // Add a new row to the data source. If a primary key field exists, you also need to consider the data generation of the primary key field and the submission of the last update.
6 Loaddata ();
7 Datarow newrow = Dsdata. Tables [ 0 ]. Newrow ();
8 Dsdata. Tables [ 0 ]. Rows. Add (newrow );
9 Dgdata. datasource = Dsdata;
10 Session [ " Mydata " ] = Dsdata;
11 // Determine whether to change pages after adding this row
12 Int Currentindex = Dgdata. Items. count;
13 If (Currentindex > = Dgdata. pagesize)
14 {
15 Dgdata. currentpageindex ++ ;
16 Currentindex =   0 ;
17 }
18 Dgdata. edititemindex = Currentindex;
19 Dgdata. databind ();
20 Dgdata. items [currentindex]. Attributes. Add ( " Isnewitem " , " True " );
21 }
22 }

Cancelcommand Event code: 1 Private   Void Dgdata_cancelcommand ( Object Source, system. Web. UI. webcontrols. datagridcommandeventargs E)
2 {
3 Loaddata ();
4 // If you select cancel in the new row, this row is not added.
5 If (E. Item. attributes [ " Isnewitem " ] ! =   Null   && E. Item. attributes [ " Isnewitem " ] =   " True " )
6 {
7 Dsdata. rejectchanges ();
8 Session [ " Mydata " ] = Dsdata;
9 }
10 Dgdata. edititemindex =   - 1 ;
11 Bindgrid (dgdata. currentpageindex );
12 }

In the preceding example, I wrote a demo. The packaged solution file can be downloaded here (Note: Modify the connection string in the web. config file and the data access code is in the dataaccess class ).
On the way to writing this post, a friend suggested placing a text box for each field in the footer of the DataGrid, and then placing a "add row" button, click the button to add the row to the DataGrid. If the footer is not used for other purposes, this method is also a good solution.

Related Article

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.