Method Analysis of a DataTable delete row in C #

Source: Internet
Author: User

The example of this paper describes the method of deleting rows in a DataTable in C # and shares it for everyone's reference. The implementation method is as follows:

Its own deletion example (Drtemp is the table, Gvsummary is Dev's gridview. Right click on grid delete):

1, DtTemp.Rows.RemoveAt (Gvsummary.focusedrowhandle);

2, Dttemp.rows[gvsummary.focusedrowhandle].  Delete (); Dttemp.acceptchanges ();

In C #, if you want to delete a row in a DataTable, there are several ways to do this:

1.

If you just want to delete a row in a DataTable, you can use the delete of the DataRow

, but it has to be deleted and let the DataTable know, so it needs to be used. AcceptChanges () method, because this deletion is just an identity delete, just like the Isdelete field we typically use in the database.

A DataTable is required after Delete (). The Acceptechanges () method confirms a complete deletion, because delete () only flags the status of the corresponding column as deleted, and it can be passed through a DataTable. RejectChanges () rollback to make the row undelete.

2.

A DataTable will be used to completely delete. Rows.remove (DataRow dr) method

, as well as just delete a row can, if you want to loop delete please continue to look down.

3.

It is necessary to use the complete cycle removal. Rows.removeat (int index) method

, so if you are a fan of foreach, please change your taste here, and if you are a loyal fan of the for i++ also hope you can change a thought. Take a look at the positive wording of the above program (wrong, not available)

for (int i = 0, j = dt. Rows.Count; I < J; i++) {  if (convert.toint32 (DT). rows[i]["RowID"]) = = RowID) dt. Rows.removeat (i);}

The mistake of this is that the RemoveAt () of the DataTable will update the index of the DataTable after deletion, so the index you want to delete may already be not your compliant convert.toint32 (dt. rows[i]["RowID"]) = = RowID Index, the very person will also throw an exception, that you visit the index does not exist.

Or use a DataTable with caution. Rows.removeat (i), to delete multiple rows, you can use Delete () consecutively and then confirm the deletion with the Acceptechanges () method.


Use the Select method:

First mark the record to be deleted, then select Delete Row, the instance code is as follows:

for (int i = 0; i < len; i++) {  if ((CheckBox) rp.items[i]. FindControl ("CB")). Checked)  {dt. rows[i]["C0"] = 1;//marks the record to be deleted  }}foreach (DataRow r in dt. Select ("C0=1")) {  r.delete ();} Rp.datasource = DT; Rp.databind ();

Interested friends can test run the example of this article to deepen understanding, I hope that this article on the C # Programming of the study has helped.

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
Method Analysis of a DataTable delete row in C #

This address: http://www.paobuke.com/develop/c-develop/pbk23491.html






Related content C # Implementation of Baidu Ping push function A simple example of using C # to write a Bluetooth communication program in Windows system to explain the inheritance in C # face object Programming C # Implementing a method of emptying the Recycle Bin
A summary of how C # implements Ping C # How to parse HTTP messages C # WinForm Control naming conventions general methods for string classes in C #

Method Analysis for DataTable delete rows in C #

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.