The difference between the delete and remove methods of a DataTable Delete Row Introduction _ Practical Tips

Source: Internet
Author: User

1. In C #, if you want to delete a row in a DataTable, there are approximately the following options:

• Use DataTable.Rows.Remove (DataRow), or DataTable.Rows.RemoveAt (index); You can delete rows directly
DataTable. Rows[i]. Delete (). The DataTable is required after Delete (). The Acceptechanges () method confirms a complete deletion, because delete () simply flags the status of the corresponding column as deleted, and can also pass through the DataTable. RejectChanges () rollback, causing the row to be removed.
• When you delete rows in a DataTable, each row is deleted, and the index of all rows in the DataTable changes. You cannot use foreach when you are looping to delete Datatable.row. When using a Foreach loop, the table is not allowed to delete and add operations.
• If the deletion is based on a criteria, each row is deleted, the index of the entire table will change immediately, equal to the table has become a new one. But the index has already added 1. This will cause the first column to never match. Therefore, each row is deleted to determine whether the first row satisfies the delete condition.
• If you want to delete multiple rows in a DataTable, you should use the reverse loop datatable.rows. The index changes as the positive sequence is deleted. It is difficult to anticipate the consequences of a program abnormality.

Summarize:

Delete and remove

The use of delete is a DataTable. Rows[i]. Delete ();
The use of Remove is Datatable.Rows.Remove (DataTable). Rows[i]);
• The difference is that when you use Delete, only the row is marked as deleted, but there is still a number of rows in Rows.Count to get the number of rows. The Datatable.acceptchanges () method needs to be used to commit the modification.
• The Remove method is deleted directly.
• If you delete a row in the For loop, it is best to use the Delete method, otherwise there will be a change in count. After the loop, use the AcceptChanges () method to commit the modification and delete the line 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.