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