DataRow.Delete Método

Definición

Elimina DataRow.Deletes the DataRow.

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

Excepciones

DataRow ya se ha eliminado.The DataRow has already been deleted.

Ejemplos

En el ejemplo siguiente se crea una simple DataTable con dos columnas y diez filas.The following example creates a simple DataTable with two columns and ten rows. Después de eliminar varios DataRow elementos con el Delete método, una de las filas se recupera mediante la invocación de 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

Comentarios

Si RowState se agrega el de la fila, se RowState convierte en Detached y la fila se quita de la tabla cuando se llama a 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.

RowStateSe convierte Deleted en después de utilizar el Delete método en un existente DataRow .The RowState becomes Deleted after you use the Delete method on an existing DataRow. Permanece Deleted hasta que se llama a AcceptChanges .It remains Deleted until you call AcceptChanges. En este momento, DataRow se quita de la tabla.At this time, the DataRow is removed from the table.

Delete no se debe llamar en un bucle foreach mientras se recorre en iteración un DataRowCollection objeto.Delete should not be called in a foreach loop while iterating through a DataRowCollection object. Delete modifica el estado de la colección.Delete modifies the state of the collection.

Una fila eliminada se puede recuperar mediante la invocación de RejectChanges .A deleted row can be undeleted by invoking RejectChanges.

Nota

El BeginEdit método suspende temporalmente los RowChanging eventos, pero no la operación de eliminación.The BeginEdit method temporarily suspends RowChanging events, but the delete operation does not.

Se aplica a

Consulte también