Multi-row commit of the DataGrid

Source: Internet
Author: User

Although ASP. net DataGrid is a well-known table control. However, when we mention the DataGrid editing function, we are not flattering. Taking the data submission function of the DataGrid for example, there is indeed a big problem: in the DataGrid, each row to be edited must submit a row, that is, "single row editing and single row submission". In this way, if there are too many lines to be edited, this is not only cumbersome, it also causes frequent access to the server, greatly reducing system efficiency.

Of course, there is a solution to the dead, that is, to transfer the content to be edited to another page and edit it in the textbox. However, if you think about it, isn't this method a lie to yourself? In the grid, when we edit it, we cannot always use the tab key to jump between grids (textbox, if the carriage return event is returned, you mustProgramDevelopers are wasting a lot of energy on development.

How can this problem be solved? Next I would like to recommend a Chinese DataGrid I'm using: smartgrid (sky software station can download: http://www.skycn.com/soft/23547.html), this control I have been using for a long time, now let's discuss with you how to submit multiple rows of smartgrid: smartgrid does not have the button columns in the DataGrid, but the entire form has only one submit button, you can submit one row or multiple rows at a time. Here is an example:

Instance:

Is an example of better editing. The example shows that you can edit multiple rows or edit one row and submit them together.

Code:

Code of the modify button:

Private void btonsave_click (Object sender, system. eventargs E)

{

This. datagrid1.readonly = false; // enter Edit

This. Maid = true; // allow to add

This. Maid = true; // Delete allowed

}

This code is a unique attribute of smartgrid. You can add and delete various editing functions.

Code for saving the button:
Private void button2_click (Object sender, system. eventargs E)

{

Datatable t = (datatable) This. smartgrid1.datasource;

This. sqldataadapter1.update (t );

T. Clear ();

This. sqldataadapter1.fill (t );

This. smartgrid1.datasource = T;

}

This is the overall process of submitting data to the database. This method is suitable for large data volumes.

Another method is to submit data row by row to the server.

Code:

Private void btonsave_click (Object sender, system. eventargs E)

{

Datatable TB = (datatable) This. smartgrid1.datasource;

Sqlparameter [] parameters = new sqlparameter [5];

Foreach (datarow DR in TB. Rows)

{

Parameters [0] = new sqlparameter ("@ customerid", "" + Dr [1] + "");

Parameters [1] = new sqlparameter ("@ companyName", "" + Dr [0] + "");

Parameters [2] = new sqlparameter ("@ contactname", "" + Dr [2] + "");

Parameters [3] = new sqlparameter ("@ contacttitle", "" + Dr [3] + "");

Parameters [4] = new sqlparameter ("@ address", "" + Dr [4] + "");

// Eampd is a class execute. It is the function that executes the stored procedure. parameters is the parameter required by the stored procedure.

Eampd. Execute ("creatlayer", parameters );

}

}

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.