Question about DataTable row Deletion

Source: Internet
Author: User

Example of Self-deletion (drTemp is a table and gvSummary is dev's gridview. Right-click grid to 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 the DataTable, there are several methods:

1. if you only want to delete a row in the datatable, you can use the delete Statement of DataRow. However, you must delete the row before letting the DataTable know, so you need to use it. the AcceptChanges () method is used because such deletion is only for identification deletion, just like the IsDelete field we usually use in the database.

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.

2. The. Rows. Remove (DataRow dr) method of the able will be used to completely delete the data. Similarly, only one row can be deleted. If you want to delete the data cyclically, continue to look down.

3. it is necessary to permanently delete the loop. rows. removeAt (int index) method, so if you are a foreach enthusiast, please change your taste here, and if you are a loyal fans of for I ++, you also hope you can change your mind. Let's take a look at the forward Writing of the above Program (incorrect, unavailable)

For (int I = 0, j = dt. Rows. Count; I <j; I ++)
{
If (Convert. ToInt32 (dt. Rows [I] ["RowID"]) = RowID)
Dt. Rows. RemoveAt (I );
}
 

The error is that the RemoveAt () of the datatable will update the index of the dataTable after deletion. Therefore, the index you want to delete may not be consistent with Convert. toInt32 (dt. rows [I] ["RowID"]) = RowID index. In addition, an exception is thrown, indicating that the index you access does not exist.

You still need to use datatable. Rows. RemoveAt (I) with caution. To Delete multiple Rows, you can use Delete () consecutively and then use the AccepteChanges () method to confirm the deletion.


The following example is found on the Internet. No

Use the select method:
Mark the record to be deleted first, then select to delete the row, OK
For (int I = 0; I <len; I ++)
{
If (CheckBox) Rp. Items [I]. FindControl ("CB"). Checked)
{
Dt. Rows [I] ["C0"] = 1; // mark the record to be deleted
}
}
Foreach (DataRow r in dt. Select ("c0 = 1 "))
{
R. Delete ();
}
Rp. DataSource = dt;
Rp. DataBind ();


From SYZ_YUMEIZHOU_YY

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.