The DataRow
object represents a single row of data within a DataTable
. It provides access to the values in each column for that specific row.
You can access the value of a specific column in a DataRow
by using the column's name or its ordinal position (index). It's recommended to use the column name for better readability and maintainability.
// Assuming 'row' is a DataRow and 'dt' is its DataTable
string customerName = row["CustomerName"].ToString();
int customerId = (int)row[0]; // Accessing by ordinal position
You can modify the values in a DataRow
before adding it to a DataTable
or updating an existing row.
row["CustomerName"] = "New Customer Name";
row[1] = 12345; // Modifying by ordinal position
Each DataRow
has a RowState
property that indicates its current status. This is crucial when working with changes and submitting them to a data source.
DataRowState.Unchanged
: The row has not been modified since it was loaded or accepted changes.DataRowState.Added
: The row has been added to the DataTable
but its changes have not yet been accepted.DataRowState.Modified
: The row has been changed since it was loaded or its changes were last accepted.DataRowState.Deleted
: The row has been marked for deletion.DataRowState.Detached
: The row is not currently associated with a DataTable
.DataRow
supports row versioning, allowing you to access the original and current versions of a row's data. This is useful for implementing optimistic concurrency.
DataRowVersion.Current
: The most recent version of the row.DataRowVersion.Original
: The version of the row before any modifications were made since it was loaded or changes were last accepted.DataRowVersion.Proposed
: The version of the row with proposed changes, typically before AcceptChanges()
is called.
// Accessing original value
string originalName = row["CustomerName", DataRowVersion.Original].ToString();
When a database column can contain null values, ADO.NET represents them using DBNull.Value
. You should check for this value before attempting to use a column's value.
if (row["Email"] != DBNull.Value)
{
string email = row["Email"].ToString();
// Process email
}
else
{
// Email is null
}
You can iterate through the rows of a DataTable
using a foreach
loop.
DataTable customersTable = dataSet.Tables["Customers"];
foreach (DataRow row in customersTable.Rows)
{
Console.WriteLine($"Customer ID: {row["CustomerID"]}, Name: {row["Name"]}");
}
The Select()
method on a DataTable
can be used to filter rows based on a filter expression.
DataRow[] foundRows = customersTable.Select("City = 'London'");
foreach (DataRow row in foundRows)
{
Console.WriteLine($"Found: {row["Name"]}");
}
Find()
method for efficient row lookup when you have a primary key defined on the DataTable
.
Member | Description |
---|---|
this[int columnIndex] |
Gets or sets the value of the column at the specified ordinal position. |
this[string columnName] |
Gets or sets the value of the column with the specified name. |
this[string columnName, DataRowVersion version] |
Gets the value of the column with the specified name and row version. |
RowError |
Gets or sets an error description for the row. |
RowState |
Gets the current state of the row. |
Table |
Gets the DataTable to which this row belongs. |