DataRow Object
The DataRow object represents a single row of data within a DataTable. It holds the values for each column in that row and provides methods and properties to access, manipulate, and manage the row's state.
Key Properties and Methods
Properties
Item(columnName)
orItem(columnIndex)
: Gets or sets the value of the specified column for this row. You can use either the column name (string) or the column index (integer).RowState
: Gets the current state of the row (e.g.,Added
,Modified
,Deleted
,Unchanged
).Table
: Gets the DataTable that contains this row.ItemArray
: Gets or sets an array containing all the values in the row.
Methods
AcceptChanges()
: Accepts any changes made to the row since the last call toAcceptChanges()
or since the row was loaded.BeginEdit()
: Begins an edit operation on the row.CancelEdit()
: Cancels the current edit operation.EndEdit()
: Ends the edit operation.Delete()
: Marks the row for deletion. The row is not actually removed untilAcceptChanges()
is called.SetAdded()
: Sets the row's state toAdded
.RejectChanges()
: Rejects any changes made to the row since the last call toAcceptChanges()
.
Working with DataRow Values
Accessing and modifying data within a DataRow
is straightforward. Here's an example:
// Assuming 'myDataTable' is a DataTable and 'myRow' is a DataRow within it
// Get a value from a column by name
string customerName = myRow["CustomerName"].ToString();
// Get a value from a column by index
int customerId = (int)myRow[0];
// Set a new value
myRow["Email"] = "new.email@example.com";
// Set multiple values using ItemArray
myRow.ItemArray = new object[] { 1, "New Product", 19.99, DateTime.Now };
Row States
Understanding the RowState
is crucial for managing data consistency, especially when dealing with updates and inserts to a data source. The possible states are:
Unchanged
: The row has not been modified since it was loaded or since the lastAcceptChanges()
call.Added
: The row has been added to theDataTable
.Modified
: The row has been changed since it was loaded or since the lastAcceptChanges()
call.Deleted
: The row has been deleted from theDataTable
.Detached
: The row is not currently associated with aDataTable
.
Note: When you call
AcceptChanges()
on a DataTable
, rows marked as Deleted
are physically removed, and rows marked as Added
or Modified
become Unchanged
.
Editing Rows
For more complex updates or to temporarily suspend changes, you can use the edit methods:
myRow.BeginEdit();
try
{
myRow["Status"] = "Processing";
myRow["LastUpdated"] = DateTime.Now;
// ... more changes
myRow.EndEdit();
}
catch (Exception ex)
{
myRow.CancelEdit(); // Revert changes if an error occurs
Console.WriteLine($"Error updating row: {ex.Message}");
}
Tip: Using
BeginEdit()
, EndEdit()
, and CancelEdit()
provides a transactional approach to modifying a single row, allowing for better error handling and rollback capabilities.
The DataRow object is a fundamental component of ADO.NET's data manipulation capabilities, enabling fine-grained control over individual records within your datasets.