DataRow.RejectChanges Method
Rejects all changes made to the row since AcceptChanges was last called.
Assembly: System.Data (in System.Data.dll)
| Exception | Condition |
|---|---|
| RowNotInTableException | The row does not belong to the table. |
When you call 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.
The following example creates a simple DataTable with 2 columns and 10 rows. After several DataRow items are deleted with the Delete method, one of the rows is undeleted by invoking RejectChanges.
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
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.