Differences between the delete and remove methods for datatable row Deletion

Source: Internet
Author: User

1. in C #, if you want to delete a row in the datatable, there are several methods:

• You can use datatable. Rows. Remove (datarow) or datatable. Rows. removeat (INDEX) to directly Delete rows.
• Datatable. Rows [I]. Delete (). The datatable is required after the delete () operation. the acceptechanges () method confirms that the data is completely deleted. Because Delete () only marks the status of the corresponding column as deleted, you can also use datatable. the rejectchanges () rollback cancels the deletion of this row.
• When deleting rows in the datatable, the indexes of all rows in the datatable change for each row deleted. You cannot use foreach when cyclically deleting able. Row. When using foreach for a loop, table deletion and addition operations are not allowed.
• If a row is deleted based on a column, the index of the entire table changes immediately after each row is deleted, which means that the table has become a new table. However, the index has already been added with 1. Therefore, the first column will never match. Therefore, after each row is deleted, determine whether the first row meets the deletion conditions.
• If you want to delete multiple rows in a able, you should use inverted loops of able. rows. This is because the index changes when the index is deleted in the forward direction. Program exceptions are difficult to predict.

Summary:

Delete and remove

• The use of Delete is datatable. Rows [I]. Delete ();
• Remove uses datatable. Rows. Remove (datatable. Rows [I]);
• The difference between the two is that after Delete is used, only the row is marked as deleted, but still exists, with rows. when count is used to obtain the number of rows, the previous number of rows is deleted. datatable is required. the acceptchanges () method to submit the modification.
• The remove method directly deletes the object.
• If you delete a row in a for loop, it is best to use the delete method, otherwise the count will change. after the loop is completed, use the acceptchanges () method to submit the changes and delete the rows marked as deleted.

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.