Datatable deletes rows and datatable deletes rows.

Source: Internet
Author: User

Datatable deletes rows and datatable deletes rows.

List the correct statements first. If you just want to correct the statements right away, copy them first,

Protected void deleteDataRow (int RowID, DataTable dt) {for (int I = dt. rows. count-1; I> = 0; I --) {if (Convert. toInt32 (dt. rows [I] ["RowID"]) = RowID) dt. rows. removeAt (I) ;}}/* He asked hovertree.com */

If you have time to learn about it, let's continue to see the following list of possible error possibilities.

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.

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);}/* He asked hovertree.com */

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.

Therefore, you need to search for and delete the row from the bottom of the DataTable, so that the row above will not be affected even if the row meets the deletion conditions.

After talking so much, do you understand? As a matter of fact, writing such an article seems a little "mentally retarded", more technical, and more I feel that my foundation is not solid enough. I hope I can record it here to urge myself, I also hope to bring some help to beginners.

Http://hovertree.com/menu/csharp/

 

ManipulationDataset
In DataSet, DataRow is the basic storage location of all its data. It is mainly composed of a value array, representing a single row of DataTable.
DataRow mainly includes the following information: 1. Current value of each column in the row, 2. original value of each column in the row, 3. Row status, 4. Links between the parent row and the Child row

Initialize a DataRow:
DataTable dataTable = dataSet. Tables [0];
DataRow newRow = able. NewRow (); // use a dataTable to generate a DataRow. The modes in the dataTable can be used.
DataTable. Rows. Add (newRow );

Delete row:
DataTable. Rows. Remove (row instance );
DataTable. Rows. RemoveAt (row number );
DataRow. Delete (); // The row itself is removed.

Read and Write DataRow values:
Row ["column name"], row [column number] can reference one of the attributes.
DataColumn a = dataTable. Columns ("column name"); // you can obtain a column

Batch modify rows:
BeginEdit () starts to change, EndEdit () ends the change, and writes the change result to DataSet, CancelEdit (), cancel the change
For example:
Row. BeginEdit ();
Modify row
Row. EndEdit ();

Http://www.cnblogs.com/roucheng/p/csfetch.html

Related Article

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.