DataRow.Delete Metoda

Definicja

Usuwa element DataRow.

C#
public void Delete ();

Wyjątki

Obiekt DataRow został już usunięty.

Przykłady

W poniższym przykładzie utworzono prosty DataTable zestaw z dwiema kolumnami i dziesięcioma wierszami. Po usunięciu kilku DataRow elementów za Delete pomocą metody jeden z wierszy jest nieukończony przez wywołanie RejectChangesmetody .

C#
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]);
    }
}

Uwagi

Jeśli wiersz RowState zostanie dodany, RowState element zostanie Detached usunięty z tabeli po wywołaniu metody AcceptChanges.

Element RowState staje się Deleted po użyciu Delete metody w istniejącym DataRowobiekcie . Pozostaje Deleted do momentu wywołania metody AcceptChanges. W tej chwili element DataRow zostanie usunięty z tabeli.

Delete nie należy wywoływać w pętli foreach podczas iterowania przez DataRowCollection obiekt. Delete modyfikuje stan kolekcji.

Usunięty wiersz można usunąć, wywołując RejectChangespolecenie .

Uwaga

Metoda BeginEdit tymczasowo zawiesza RowChanging zdarzenia, ale operacja usuwania nie.

Dotyczy

Produkt Wersje
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Zobacz też