Namespace: System.Data
Assembly: System.Data.dll
The DataRow class represents a single row in a DataTable. It provides methods for accessing, modifying, and navigating column values.
public sealed class DataRow : object, ICustomTypeDescriptor, ISupportInitialize, ISerializable, IXmlSerializable
| Property | Type | Description |
|---|---|---|
Item | object | Gets or sets the value of a column in the row using column name or index. |
Table | DataTable | Gets the DataTable that contains the row. |
RowState | DataRowState | Gets the state of the row (Added, Modified, Deleted, etc.). |
HasVersion | bool | Indicates whether the row has a version of data for the specified DataRowVersion. |
IsNull | bool | Checks if a column value is null. |
| Method | Signature | Description |
|---|---|---|
BeginEdit | void BeginEdit() | Begins an edit operation on the row. |
CancelEdit | void CancelEdit() | Cancels the edit operation. |
EndEdit | void EndEdit() | Ends the edit operation and commits changes. |
Delete | void Delete() | Deletes the row from its table. |
AcceptChanges | void AcceptChanges() | Commits all the changes made to the row. |
RejectChanges | void RejectChanges() | Reverts the row to its original state. |
SetAdded | void SetAdded() | Marks the row as added. |
SetModified | void SetModified() | Marks the row as modified. |
DataRow objects are usually created by the DataTable.NewRow method. Each row maintains an internal state that tracks changes, which is useful for data-binding and update scenarios.
using System;
using System.Data;
class Program
{
static void Main()
{
DataTable table = new DataTable("Customers");
table.Columns.Add("Id", typeof(int));
table.Columns.Add("Name", typeof(string));
DataRow row = table.NewRow();
row["Id"] = 1;
row["Name"] = "Alice";
table.Rows.Add(row);
// Modify the row
row.BeginEdit();
row["Name"] = "Alice Smith";
row.EndEdit();
Console.WriteLine($"Name: {row["Name"]}, State: {row.RowState}");
}
}