Home > Backend Development > C#.Net Tutorial > C# Table.Rows[index].Delete()

C# Table.Rows[index].Delete()

黄舟
Release: 2017-02-21 11:07:15
Original
1685 people have browsed it

If the DataRow.Delete() method is called, the RowState property of the corresponding row will be set to DataRowState.Deleted. When the status of a row is DataRowState.Deleted, we cannot access this row under normal circumstances. If you need to retrieve deleted original data, you can use the overloaded method of its entry property to pass the DataRowVersion.Original parameter.
The code is like this: ​

if (typedDataRow.RowState == DataRowState.Deleted)
id = (string)typedDataRow["CustomerID", DataRowVersion.Original];
Copy after login

​ Use datatable.Rows[i].Delete() to delete rows or press the Del key on the DataGrid to delete rows and then access the table again. "Cannot pass deleted rows" appears. row to access the row's information" error. The reasons are as follows:
After Delete(), the datatable.AccepteChanges() method is required to confirm the complete deletion, because Delete() only marks the status of the corresponding column as deleted, and can also be rolled back through datatable.RejectChanges() to cancel the row. delete.
So If you want to completely delete the datarow, you need to use the Delete() and AccepteChanges() methods at the same time , or use the datatable.Rows.RemoveAt(i) method to directly delete , where i Represents the row index, and another one is datatable.Rows.Remove(DataRow dr) to delete the specified row.
However, when using datatable.Rows.RemoveAt(i), please note that if you use datatable.Rows.RemoveAt(0); datatable.Rows.RemoveAt(1); continuously, you will not delete 0, 1 in the original table. rows, but after deleting 0 rows, the original 1 row becomes 0 rows, so datatable.Rows.RemoveAt(1) actually deletes 2 rows of the original table. At this time it should be i = i - 1;

So you should use datatable.Rows.RemoveAt(i) with caution

Small note:

Row version (DataRowVersion )
The row (DataRow) object has four different row versions, namely Current, Original, proposed and default. A brief description of these row versions follows.
Current: Indicates the current value of the row. No version of the row exists for a row in the deleted state.
Original: Represents the original value of the row. The row version in the Added state does not exist
Proposed: Indicates the recommended value of the row. This row version exists for rows that do not belong to the table, that is, rows in the Detached state; this row version also exists for rows that are being edited.
Default: Indicates the default version of the row. The default row version for rows in the Added, Modified, or Unchanged state is current; for rows in the deleted state, the default row version is original. The default version for rows in the Detached state is proposed.

The above is the content of C# Table.Rows[index].Delete(). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template