Export (0) Print
Expand All

DataRow.RejectChanges Method

Rejects all changes made to the row since AcceptChanges was last called.

[Visual Basic]
Public Sub RejectChanges()
[C#]
public void RejectChanges();
[C++]
public: void RejectChanges();
[JScript]
public function RejectChanges();

Exceptions

Exception Type Condition
RowNotInTableException The row doesn't belong to the table.

Remarks

When calling the RejectChanges method, the CancelEdit method is implicitly called to cancel any edits. If RowState is Deleted or Modified, the row reverts to its previous values, and RowState becomes Unchanged. If the RowState is Added, the row is removed.

Example

[Visual Basic, C#, C++] The following example creates a simple DataTable with two columns and ten rows. After deleting several DataRow items with the Delete method, one of the rows is undeleted by invoking RejectChanges.

[Visual Basic] 
Private Sub DemonstrateDeleteRow()
    ' Create a simple DataTable with two columns and ten rows.
    Dim myTable As New DataTable("myTable")
    Dim c1 As New DataColumn("id", Type.GetType("System.Int32"))
    c1.AutoIncrement = True
    Dim c2 As New DataColumn("item", Type.GetType("System.String"))
    myTable.Columns.Add(c1)
    myTable.Columns.Add(c2)
    ' Add ten rows.
    Dim newRow As DataRow
    
    Dim i As Integer
    For i = 0 To 9
        newRow = myTable.NewRow()
        newRow("item") = "Item " + i.ToString()
        myTable.Rows.Add(newRow)
    Next i
    myTable.AcceptChanges()
    ' Create a DataView with the table.
    Dim rc As DataRowCollection = myTable.Rows
    rc(0).Delete()
    rc(2).Delete()
    rc(3).Delete()
    rc(5).Delete()
    Console.WriteLine(rc(3).RowState.ToString())
    ' Reject changes on one deletion.
    rc(3).RejectChanges()
    ' Change the value of the column so it stands out.
    rc(3)("item") = "Deleted, Undeleted, Edited"
    ' Accept changes on others.
    myTable.AcceptChanges()
    ' Print the remaining row values.
    Dim r As DataRow
    For Each r In  myTable.Rows
        Console.WriteLine(r(0).ToString() + ControlChars.Tab + r(1).ToString())
    Next r
End Sub

[C#] 
private void DemonstrateDeleteRow(){
   // Create a simple DataTable with two columns and ten rows.
   DataTable myTable = new DataTable("myTable");
   DataColumn c1 = new DataColumn("id",Type.GetType("System.Int32"));
   c1.AutoIncrement=true;
   DataColumn c2 = new DataColumn("item", Type.GetType("System.String"));
   myTable.Columns.Add(c1);
   myTable.Columns.Add(c2);
   // Add ten rows.
   DataRow newRow;
        
   for(int i = 0; i <10; i++){
      newRow = myTable.NewRow();
      newRow["item"] = "Item " + i;
      myTable.Rows.Add(newRow);
   }
   myTable.AcceptChanges();
   // Create a DataView with the table.
   DataRowCollection rc = myTable.Rows;
   rc[0].Delete();
   rc[2].Delete();
   rc[3].Delete();
   rc[5].Delete();
   Console.WriteLine(rc[3].RowState.ToString());
   // Reject changes on one deletion.
   rc[3].RejectChanges();
   // Change the value of the column so it stands out.
   rc[3]["item"] = "Deleted, Undeleted, Edited";
   // Accept changes on others.
   myTable.AcceptChanges();
   // Print the remaining row values.
   foreach(DataRow r in myTable.Rows){
      Console.WriteLine(r[0] + "\t" + r[1]);
   }
}

[C++] 
private:
 void DemonstrateDeleteRow(){
    // Create a simple DataTable with two columns and ten rows.
    DataTable* myTable = new DataTable(S"myTable");
    DataColumn* c1 = new DataColumn(S"id",Type::GetType(S"System.Int32"));
    c1->AutoIncrement=true;
    DataColumn* c2 = new DataColumn(S"item", Type::GetType(S"System.String"));
    myTable->Columns->Add(c1);
    myTable->Columns->Add(c2);
    // Add ten rows.
    DataRow* newRow;
         
    for(int i = 0; i <10; i++){
       newRow = myTable->NewRow();
       newRow->Item[S"item"] = String::Format( S"Item {0}", __box(i));
       myTable->Rows->Add(newRow);
    }
    myTable->AcceptChanges();
    // Create a DataView with the table.
    DataRowCollection* rc = myTable->Rows;
    rc->Item[0]->Delete();
    rc->Item[2]->Delete();
    rc->Item[3]->Delete();
    rc->Item[5]->Delete();
    Console::WriteLine(rc->Item[3]->RowState);
    // Reject changes on one deletion.
    rc->Item[3]->RejectChanges();
    // Change the value of the column so it stands out.
    rc->Item[3]->Item[S"item"] = S"Deleted, Undeleted, Edited";
    // Accept changes on others.
    myTable->AcceptChanges();
    // Print the remaining row values.
    System::Collections::IEnumerator* myEnum = myTable->Rows->GetEnumerator();
    while (myEnum->MoveNext())
    {
       DataRow* r = __try_cast<DataRow*>(myEnum->Current);
       Console::WriteLine(S"{0}\t{1}", r->Item[0], r->Item[1]);
    }
 }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

DataRow Class | DataRow Members | System.Data Namespace | AcceptChanges | IsNull

Show:
© 2014 Microsoft