9.4 Using Row State Information

The RowState property is used by ADO.NET to track the changes that have been made to a DataRow, which allows changes made to the data while disconnected to be updated back to the data source. The RowState property indicates whether the row belongs to a table, and if it does, whether it's newly inserted, modified, deleted, or unchanged since it was loaded.

The value of the RowState property can't be set directly. ADO.NET sets the row state in response to actions that affect the DataRow. The AcceptChanges( ) and RejectChanges( ) methods, whether explicitly or implicitly called, both reset the RowState value for the row to Unchanged. The following code illustrates this idea:

// create a table with one column
DataTable dt = new DataTable();
dt.Columns.Add("MyColumn",  typeof(System.String));

// create a new row
DataRow row = dt.NewRow();        // RowState = Detached

// add the row to the table
dt.Rows.Add(row);                 // RowState = Added
dt.AcceptChanges();               // RowState = Unchanged

// modify the row
row["MyColumn"] = "MyFieldValue"; // RowState = Modified

// reject the changes
row.RejectChanges();              // RowState = Unchanged

// delete the row
row.Delete();                     // RowState = Deleted
row.AcceptChanges();              // row no longer exists

    Part I: ADO.NET Tutorial
    Part II: ADO.NET Core Classes
    Part III: API Quick Reference