Row States and Row Versions

ADO.NET manages rows in tables using row states and versions. A row state indicates the status of a row. Row versions maintain the values stored in a row as it is modified including current, original, and default values. For example, after you have made a modification to a column in a row, the row will have a row state of Modified, and two row versions will exist: Current, which contains the current row values, and Original, which contains the row values before the column was modified.

Each DataRow object has a RowState property that you can examine to determine the current state of the row. The following table gives a brief description of each RowState enumeration value.

RowState Description
Unchanged No changes have been made since the last call to AcceptChanges or since the row was created by DataAdapter.Fill.
Added The row has been added to the table, but AcceptChanges has not been called.
Modified Some element of the row has been changed.
Deleted The row has been deleted from a table and AcceptChanges has not been called.
Detached Detached is set for a row that has been created but is not part of any DataRowCollection. The RowState of a newly created row is set to Detached. After the new DataRow is added to the DataRowCollection by calling the Add method, the value of the RowState property is set to Added.

Detached is also set for a row that has been removed from a DataRowCollection using the Remove method, or by the Delete method followed by the AcceptChanges method.

When AcceptChanges is called on a DataSet, DataTable, or DataRow, all rows with a row state of Deleted are removed. The remaining rows are given a row state of Unchanged and the values in the Original row version are overwritten with the Current row version values. When RejectChanges is called, all rows with a row state of Added are removed. The remaining rows are given a row state of Unchanged and the values in the Current row version are overwritten with the Original row version values.

You can view the different row versions of a row by passing a DataRowVersion parameter with the column reference, as shown in the following example.

Dim custRow As DataRow = custTable.Rows(0)
Dim custID As String = custRow("CustomerID", DataRowVersion.Original).ToString()
[C#]
DataRow custRow = custTable.Rows[0];
string custID = custRow["CustomerID", DataRowVersion.Original].ToString();

The following table gives a brief description of each DataRowVersion enumeration value.

DataRowVersion Description
Current The current values for the row. This row version does not exist for rows with a RowState of Deleted.
Default The default row version for a particular row. The default row version for an Added, Modified, or Unchanged row is Current. The default row version for a Deleted row is Original. The default row version for a Detached row is Proposed.
Original The original values for the row. This row version does not exist for rows with a RowState of Added.
Proposed The proposed values for the row. This row version exists during an edit operation on a row, or for a row that is not part of a DataRowCollection.

You can test whether a DataRow has a particular row version by calling the HasVersion method and passing a DataRowVersion as an argument. For example, DataRow.HasVersion(DataRowVersion.Original) will return false for newly added rows before AcceptChanges has been called.

The following code example displays the values in all the deleted rows of a table. Deleted rows do not have a Current row version, so you must pass DataRowVersion.Original when accessing the column values.

Dim catTable As DataTable = catDS.Tables("Categories")

Dim delRows() As DataRow = catTable.Select(Nothing, Nothing, DataViewRowState.Deleted)

Console.WriteLine("Deleted rows:" & vbCrLf)

Dim catCol As DataColumn
Dim delRow As DataRow

For Each catCol In catTable.Columns
  Console.Write(catCol.ColumnName & vbTab)
Next
Console.WriteLine()

For Each delRow In delRows
  For Each catCol In catTable.Columns
    Console.Write(delRow(catCol, DataRowVersion.Original) & vbTab)
  Next
  Console.WriteLine()
Next
[C#]
DataTable catTable = catDS.Tables["Categories"];

DataRow[] delRows = catTable.Select(null, null, DataViewRowState.Deleted);

Console.WriteLine("Deleted rows:\n");

foreach (DataColumn catCol in catTable.Columns)
  Console.Write(catCol.ColumnName + "\t");
Console.WriteLine();

foreach (DataRow delRow in delRows)
{
  foreach (DataColumn catCol in catTable.Columns)
    Console.Write(delRow[catCol, DataRowVersion.Original] + "\t");
  Console.WriteLine();
}

See Also

Manipulating Data in a DataTable | DataRowCollection Class | DataRow.RowState Property | DataRowVersion Enumeration | DataViewRowState Enumeration