Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

DataRow.AcceptChanges Method

Commits all the changes made to this row since the last time AcceptChanges was called.

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

Exceptions

Exception Type Condition
RowNotInTableException The row does not belong to the table.

Remarks

When invoking AcceptChanges, the EndEdit method is implicitly called to end any edits. If the RowState of the row was Added or Modified, the tee cref="System.Data.DataRow.RowState"/> becomes Unchanged. If the RowState was Deleted, the row is removed.

See the BeginEdit method for more details.

The DataTable class also has an AcceptChanges method which affects changes made to the entire table.

Example

[Visual Basic, C#, C++] The following example first creates a new DataTable with one column, then creates a single DataRow. As the DataRow is created, added, modified, and deleted, its RowState is printed.

[Visual Basic] 
Private Sub DemonstrateAcceptChanges()
    ' Run a function to create a DataTable with one column.
    Dim myTable As DataTable = MakeTable()
    Dim myRow As DataRow 
 
    ' Create a new DataRow.
    myRow = myTable.NewRow()
    ' Detached row.
    Console.WriteLine("New Row " & myRow.RowState)
 
    myTable.Rows.Add(myRow)
    ' New row.
    Console.WriteLine("AddRow " & myRow.RowState)
 
    myTable.AcceptChanges()
    ' Unchanged row.
    Console.WriteLine("AcceptChanges " & myRow.RowState)
 
    myRow("FirstName") = "Scott"
    ' Modified row.
    Console.WriteLine("Modified " & myRow.RowState)
 
    myRow.Delete()
    ' Deleted row.
    Console.WriteLine("Deleted " & myRow.RowState)
 End Sub
 
 Private Function MakeTable()As DataTable
    ' Make a simple table with one column.
    Dim dt As DataTable = New DataTable("myTable")
    Dim dcFirstName As DataColumn = New DataColumn("FirstName", Type.GetType("System.String"))
    dt.Columns.Add(dcFirstName)
    MakeTable = dt
 End Function

[C#] 
private void DemonstrateAcceptChanges(){
    //Run a function to create a DataTable with one column.
    DataTable myTable = MakeTable();
    DataRow myRow;
 
    // Create a new DataRow.
    myRow = myTable.NewRow();
    // Detached row.
    Console.WriteLine("New Row " + myRow.RowState);
 
    myTable.Rows.Add(myRow);
    // New row.
    Console.WriteLine("AddRow " + myRow.RowState);
 
    myTable.AcceptChanges();
    // Unchanged row.
    Console.WriteLine("AcceptChanges " + myRow.RowState);
 
    myRow["FirstName"] = "Scott";
    // Modified row.
    Console.WriteLine("Modified " + myRow.RowState);
 
    myRow.Delete();
    // Deleted row.
    Console.WriteLine("Deleted " + myRow.RowState);
 }
 
 private DataTable MakeTable(){
    // Make a simple table with one column.
    DataTable dt = new DataTable("myTable");
    DataColumn dcFirstName = new DataColumn("FirstName", Type.GetType("System.String"));
    dt.Columns.Add(dcFirstName);
    return dt;
 }

[C++] 
private:
void DemonstrateAcceptChanges(){
    //Run a function to create a DataTable with one column.
    DataTable* myTable = MakeTable();
    DataRow* myRow;
 
    // Create a new DataRow.
    myRow = myTable->NewRow();
    // Detached row.
    Console::WriteLine(S"New Row {0}", __box(myRow->RowState));
 
    myTable->Rows->Add(myRow);
    // New row.
    Console::WriteLine(S"AddRow {0}", __box(myRow->RowState));
 
    myTable->AcceptChanges();
    // Unchanged row.
    Console::WriteLine(S"AcceptChanges {0}", __box(myRow->RowState));
 
    myRow->Item[S"FirstName"] = S"Scott";
    // Modified row.
    Console::WriteLine(S"Modified {0}", __box(myRow->RowState));
 
    myRow->Delete();
    // Deleted row.
    Console::WriteLine(S"Deleted {0}", __box(myRow->RowState));
 }
 
 DataTable* MakeTable(){
    // Make a simple table with one column.
    DataTable* dt = new DataTable(S"myTable");
    DataColumn* dcFirstName = new DataColumn(S"FirstName", Type::GetType(S"System.String"));
    dt->Columns->Add(dcFirstName);
    return dt;
 }

[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 | BeginEdit | CancelEdit | DataViewRowState | DataTable | EndEdit | HasVersion | Item | RowState

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft