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 행의 추가 합니다 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.

합니다 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.

적용 대상

추가 정보