9.6 Accepting or Rejecting Changes to Rows

9.6 Accepting or Rejecting Changes to Rows

When a DataRow is modified, ADO.NET marks the row as having a pending change and sets the RowState of the DataRow to Added , Modified , or Deleted as appropriate. ADO.NET also maintains version information by tracking both Original and Current versions of each row. Together, this information allows ADO.NET to identify both the rows and columns that have been changed.

The DataRow has three methods GetChanges( ) , AcceptChanges( ) , and RejectChanges( ) that can commit or discard the changes made to a DataRow since it was last loaded or since AcceptChanges( ) was last called. These methods function identically to the same methods for the DataSet that are described in Chapter 6. Also, when these methods are called on a DataSet , they are implicitly called on all tables contained within the DataSet and from there are called implicitly on all rows in the table.

9.7 Navigating Parent and Child Rows

A DataRow can have both parent and child rows if the DataTable that it belongs to has a DataRelation set up with another table. There are three methods that allow these relationships to be navigated. The GetParentRow( ) method returns the parent row as a DataRow object. The GetParentRows( ) method returns the parent rows as an array of DataRow objects. Both methods require either the name of the DataRelation or a reference to the DataRelation as an argument. An optional argument specifying the DataRowVersion allows control over the version of the rows returned.

The SetParentRow( ) method changes the parent row for the DataRow . This method simply takes a reference to the new parent DataRow and an optional DataRelation argument, if required.

Finally, the GetChildRows( ) method returns the child rows as an array of DataRow objects. The name of the DataRelation or a reference to the DataRelation is required as an argument. An optional argument specifying the DataRowVersion allows control over the version of rows returned.

Navigating parent and child rows is explored in detail in Chapter 11.

9.8 Using Row Error Information

The HasErrors property returns a Boolean value indicating whether an error is set on any of the columns in the row. This value can determine whether error-handling code needs to be executed or set custom error information based on custom validation rules. Rather than iterating over the entire collection of rows to locate errors, the GetErrors( ) method of the DataTable can return the array of rows containing errors within the table. The HasErrors property of the DataTable indicates whether there are any rows with errors and should be checked to determine whether calling GetErrors( ) is necessary.

Error information for the row can be set for the entire row or for a specific column in the row. The RowError property sets and retrieves an error description that applies to the entire row. The GetColumnError( ) and SetColumnError( ) methods set and get the error description for a particular column specified with either a column name , column ordinal, or a reference to the column object.

Finally, the ClearErrors( ) method clears all error information for the row and for all columns in the row.

The following example demonstrates how to work with DataRow errors:

 DataRow row;

// ... code to set up the row

if(row.HasErrors)
{
    String rowErrorText = row.RowError;
    
    foreach(DataColumn colError in row.GetColumnsInError())
    {
        String colErrorColumnName = colError.ColumnName;
        String colErrorText = row.GetColumnError(colError);

        // ... processing for column errors
    }

    // ... processing for row error

    // clear the errors, once processed
    row.ClearErrors();
}
else
{
    // no errors in the row
} 

As mentioned, errors can also be set on columns, as shown in this example:

 DataRow row;

// ... code to set up the row

// using the column name
row.SetColumnError("MyColumn", "Custom error text, based on name.");

// using the ordinal
row.SetColumnError(1, "Custom error text, based on ordinal.");