DataRow.Delete 메서드

정의

DataRow를 삭제합니다.Deletes the DataRow.

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

예외

DataRow가 이미 삭제된 경우The DataRow has already been deleted.

예제

다음 예에서는 DataTable 두 개의 열과 10 개의 행이 있는 간단한를 만듭니다.The following example creates a simple DataTable with two columns and ten rows. 메서드를 사용 하 여 여러 항목을 삭제 한 후에는를 DataRow Delete 호출 하 여 행 중 하나를 삭제 취소 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

설명

RowState행의이 추가 되 면가가 RowStateDetached 고를 호출할 때 테이블에서 행이 제거 됩니다 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.

RowState Deleted 기존에서 메서드를 사용 하 게 됩니다 Delete DataRow .The RowState becomes Deleted after you use the Delete method on an existing DataRow. Deleted를 호출할 때까지 유지 됩니다 AcceptChanges .It remains Deleted until you call AcceptChanges. 이번에는 DataRow 테이블에서가 제거 됩니다.At this time, the DataRow is removed from the table.

Delete 개체를 반복 하는 동안 foreach 루프에서를 호출 하면 안 됩니다 DataRowCollection .Delete 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.

참고

BeginEdit메서드는 이벤트를 일시적으로 일시 중단 RowChanging 하지만 삭제 작업은 수행 하지 않습니다.The BeginEdit method temporarily suspends RowChanging events, but the delete operation does not.

적용 대상

추가 정보