刪除 DataRowDataRow Deletion

有兩種方法可用來刪除DataRow DataTable物件中的物件: DataRowCollection物件的Remove Delete方法,以及DataRow物件的方法。There are two methods you can use to delete a DataRow object from a DataTable object: the Remove method of the DataRowCollection object, and the Delete method of the DataRow object. 方法會從DataRowCollection Delete刪除DataRow ,而方法只會標示要刪除的資料列。 RemoveWhereas the Remove method deletes a DataRow from the DataRowCollection, the Delete method only marks the row for deletion. 當應用程式呼叫AcceptChanges方法時,就會發生實際的移除。The actual removal occurs when the application calls the AcceptChanges method. 您可以使用 Delete,以程式設計方式檢查有哪些資料列標示為要刪除,然後再將其實際移除。By using Delete, you can programmatically check which rows are marked for deletion before actually removing them. 將資料列標記為要刪除時,會將其 RowState 屬性設為 DeleteWhen a row is marked for deletion, its RowState property is set to Delete.

在 foreach 迴圈中不應呼叫 DeleteRemove,而應逐一查看 DataRowCollection 物件。Neither Delete nor Remove should be called in a foreach loop while iterating through a DataRowCollection object. DeleteRemove 都不會修改集合的狀態。Delete nor Remove modify the state of the collection.

DataSet當使用或DataTable搭配DataAdapter和關聯式資料來源時,請使用DataRowDelete方法來移除資料列。When using a DataSet or DataTable in conjunction with a DataAdapter and a relational data source, use the Delete method of the DataRow to remove the row. Delete方法會將資料列標示為已在DataSetDataTable刪除,但不會將它移除。The Delete method marks the row as Deleted in the DataSet or DataTable but does not remove it. 相反地,當DataAdapter遇到標記為已刪除的資料列時,它會執行其DeleteCommand方法,以刪除資料來源中的資料列。Instead, when the DataAdapter encounters a row marked as Deleted, it executes its DeleteCommand method to delete the row at the data source. 然後,您可以使用AcceptChanges方法來永久移除該資料列。The row can then be permanently removed using the AcceptChanges method. 如果您使用 [移除] 來刪除資料列,就會從資料表中移除該資料列,但DataAdapter不會刪除資料來源中的資料列。If you use Remove to delete the row, the row is removed entirely from the table, but the DataAdapter will not delete the row at the data source.

DataRowCollectionRemove方法會採用DataRow做為引數,並將它從集合中移除,如下列範例所示。The Remove method of the DataRowCollection takes a DataRow as an argument and removes it from the collection, as shown in the following example.

workTable.Rows.Remove(workRow)  
workTable.Rows.Remove(workRow);  

相反地,下列範例將示範如何在DataRow上呼叫Delete方法,以將其RowState變更為DeletedIn contrast, the following example demonstrates how to call the Delete method on a DataRow to change its RowState to Deleted.

workRow.Delete  
workRow.Delete();  

如果將資料列標示為要刪除,而且您呼叫datatable物件的AcceptChanges方法,則會從datatable移除該資料列。If a row is marked for deletion and you call the AcceptChanges method of the DataTable object, the row is removed from the DataTable. 相反地,如果您呼叫RejectChanges,則資料列的RowState會還原為標示為已刪除之前的內容。In contrast, if you call RejectChanges, the RowState of the row reverts to what it was before being marked as Deleted.

注意

如果加入DataRowRowState ,表示它剛加入至資料表,然後將它標示為已刪除,則會從資料表中移除它。If the RowState of a DataRow is Added, meaning it has just been added to the table, and it is then marked as Deleted, it is removed from the table.

另請參閱See also