DataRow Class

Represents a row of data within a DataTable.

Introduction

The DataRow class represents a single row of data within a DataTable. It is the fundamental unit for storing and manipulating data in a disconnected dataset. Each DataRow contains values for the columns defined in its associated DataTable.

DataRow objects are accessed through the Rows collection of a DataTable. You can retrieve a specific DataRow by its index or iterate through the collection to process all rows.

Key Properties and Methods

The DataRow class offers several important properties and methods for interacting with row data:

Properties

  • Item(int columnIndex) / Item(string columnName): Gets or sets the value of the specified column in the row.
  • RowError: Gets or sets a string representing an error description for the row.
  • Table: Gets the DataTable that contains this row.
  • RowState: Gets the current state of the row (Added, Deleted, Modified, Detached, Unchanged).

Methods

  • AcceptChanges(): Commits all changes made to the row since the last call to AcceptChanges.
  • BeginEdit(): Begins an edit operation on the row.
  • CancelEdit(): Cancels the current edit operation on the row.
  • Delete(): Marks the row for deletion.
  • GetChildRows(DataRelation relation): Gets the child rows for this row based on the specified DataRelation.
  • GetParentRow(DataRelation relation): Gets the parent row for this row based on the specified DataRelation.
  • SetParentRow(DataRow parentRow): Sets the parent row for this row.

Working with DataRow Values

You can access and modify column values using the Item property, either by column index or by column name. It's important to handle potential DBNull.Value when retrieving data.

Example: Accessing and setting row data

// Assuming 'dt' is a DataTable and 'dr' is a DataRow from its Rows collection // Assuming 'dt' has columns "ProductID" (int) and "ProductName" (string) // Accessing values object productIdValue = dr["ProductID"]; if (productIdValue != DBNull.Value) { int productId = (int)productIdValue; Console.WriteLine($"Product ID: {productId}"); } string productName = dr["ProductName"].ToString(); Console.WriteLine($"Product Name: {productName}"); // Setting values dr["ProductName"] = "New Product Name"; dr["Price"] = 19.99; // Assuming a "Price" column exists // Handling null values explicitly if (dr["Description"] == DBNull.Value) { dr["Description"] = "No description available."; }

Row States

The RowState property indicates the lifecycle of a DataRow:

  • Added: The row has been added to the DataTable but its changes have not yet been accepted.
  • Deleted: The row has been deleted from the DataTable.
  • Modified: The row's data has been changed but the changes have not yet been accepted.
  • Detached: The row is not associated with a DataTable.
  • Unchanged: The row has not been modified since it was loaded or since the last call to AcceptChanges.

You can use AcceptChanges() to commit pending changes and reset the RowState to Unchanged. RejectChanges() can be used to revert changes.

Error Handling

You can assign an error message to a row using the RowError property. This is useful for indicating data validation failures or other issues specific to a row.

Example: Setting row error

if (!IsValidProduct(dr)) // Assume IsValidProduct is a custom validation method { dr.RowError = "Product data is invalid. Please review."; }

You can check for row errors before accepting changes or when iterating through rows.