DataRow.Delete Method

Definition

删除 DataRowDeletes the DataRow.

public:
 void Delete();
public void Delete ();
member this.Delete : unit -> unit
Public Sub Delete ()

Exceptions

已删除 DataRowThe DataRow has already been deleted.

Examples

下面的示例创建一个具有两列和10行的简单 DataTableThe following example creates a simple DataTable with two columns and ten rows. 使用 Delete 方法删除多个 DataRow 项后,将通过调用 RejectChanges取消删除某行。After deleting several DataRow items with the Delete method, one of the rows is undeleted by invoking RejectChanges.

private void DemonstrateDeleteRow()
{
    // Create a simple DataTable with two columns and ten rows.
    DataTable table = new DataTable("table");
    DataColumn idColumn = new DataColumn("id",
        Type.GetType("System.Int32"));
    idColumn.AutoIncrement=true;
    DataColumn itemColumn = new DataColumn("item",
        Type.GetType("System.String"));
    table.Columns.Add(idColumn);
    table.Columns.Add(itemColumn);

    // Add ten rows.
    DataRow newRow;

    for(int i = 0; i <10; i++)
    {
        newRow = table.NewRow();
        newRow["item"] = "Item " + i;
        table.Rows.Add(newRow);
    }
    table.AcceptChanges();

    DataRowCollection itemColumns = table.Rows;
    itemColumns[0].Delete();
    itemColumns[2].Delete();
    itemColumns[3].Delete();
    itemColumns[5].Delete();
    Console.WriteLine(itemColumns[3].RowState.ToString());

    // Reject changes on one deletion.
    itemColumns[3].RejectChanges();

    // Change the value of the column so it stands out.
    itemColumns[3]["item"] = "Deleted, Undeleted, Edited";

    // Accept changes on others.
    table.AcceptChanges();

    // Print the remaining row values.
    foreach(DataRow row in table.Rows)
    {
        Console.WriteLine(row[0] + "\table" + row[1]);
    }
}
 Private Sub DemonstrateDeleteRow()
     ' Create a simple DataTable with two columns and ten rows.
     Dim table As New DataTable("table")
     Dim idColumn As New DataColumn("id", Type.GetType("System.Int32"))
     idColumn.AutoIncrement = True
     Dim itemColumn As New DataColumn("item", Type.GetType("System.String"))
     table.Columns.Add(idColumn)
     table.Columns.Add(itemColumn)

     ' Add ten rows.
     Dim newRow As DataRow
     
     Dim i As Integer
     For i = 0 To 9
         newRow = table.NewRow()
         newRow("item") = "Item " & i.ToString()
         table.Rows.Add(newRow)
     Next i
     table.AcceptChanges()

     Dim itemColumns As DataRowCollection = table.Rows
     itemColumns(0).Delete()
     itemColumns(2).Delete()
     itemColumns(3).Delete()
     itemColumns(5).Delete()
     Console.WriteLine(itemColumns(3).RowState.ToString())

     ' Reject changes on one deletion.
     itemColumns(3).RejectChanges()

     ' Change the value of the column so it stands out.
     itemColumns(3)("item") = "Deleted, Undeleted, Edited"

     ' Accept changes on others.
     table.AcceptChanges()

     ' Print the remaining row values.
     Dim row As DataRow
     For Each row In  table.Rows
         Console.WriteLine(row(0).ToString() & ControlChars.Tab _
            & row(1).ToString())
     Next row
End Sub

Remarks

如果添加了行的 RowState,则 RowState 将变为 Detached,并且当调用 AcceptChanges时,将从表中删除该行。If the RowState of the row is Added, the RowState becomes Detached and the row is removed from the table when you call AcceptChanges.

在现有 DataRow上使用 Delete 方法之后,RowState 将变为 DeletedThe RowState becomes Deleted after you use the Delete method on an existing DataRow. 在调用 AcceptChanges之前,它将保持 DeletedIt remains Deleted until you call AcceptChanges. 此时,将从表中删除 DataRowAt this time, the DataRow is removed from the table.

在循环访问 DataRowCollection 对象时,不应在 foreach 循环中调用 DeleteDelete should not be called in a foreach loop while iterating through a DataRowCollection object. Delete 修改集合的状态。Delete modifies the state of the collection.

可以通过调用 RejectChanges删除删除的行。A deleted row can be undeleted by invoking RejectChanges.

Note

BeginEdit 方法暂时挂起 RowChanging 事件,但删除操作不会。The BeginEdit method temporarily suspends RowChanging events, but the delete operation does not.

Applies to

See also