共用方式為


DataRow.Delete 方法

定義

刪除 DataRow

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

例外狀況

範例

下列範例會建立具有兩個數據行和十個數據列的簡單 DataTable 。 使用 Delete 方法刪除數 DataRow 個專案之後,叫用 會取消 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

備註

RowState如果資料列的 是 [新增],當您呼叫 AcceptChanges 時, RowState 會變成 Detached ,而且該資料列會從資料表中移除。

會在 RowStateDeleted 您在 Delete 現有 DataRow 上使用 方法之後變成 。 它會維持不變, Deleted 直到您呼叫 AcceptChanges 為止。 此時, DataRow 會從資料表中移除 。

Delete 在逐一查看 物件時,不應該在 foreach 迴圈中呼叫 DataRowCollectionDelete 修改集合的狀態。

叫用 即可取消 RejectChanges 刪除的資料列。

注意

方法 BeginEdit 會暫時暫止 RowChanging 事件,但刪除作業不會。

適用於

另請參閱