DataRow Class (System.Data)
Namespace: System.DataOn this page
Introduction
Represents a row of data in a DataTable
. A DataRow
is a member of the DataRowCollection
, which is accessed through the Rows
property of a DataTable
.
You can access the values of columns within a DataRow
by index or by column name.
DataRow
object is created only when you add a new row to a DataTable
using the DataTable.NewRow
method or by using methods like DataTable.Rows.Add
. You cannot create a DataRow
object directly.
Syntax
public sealed class DataRow : MarshalByRefObject, ICloneable
Remarks
The DataRow
object holds the data for a single record. It is intrinsically linked to its parent DataTable
and its schema.
Each DataRow
has a state that indicates whether it is in an Added
, Modified
, Deleted
, or Unchanged
state. You can check the RowState
property to determine the current state.
When modifying a DataRow
, it's good practice to use the BeginEdit
and EndEdit
methods. This allows for transactional editing and enables handling of the RowChanging
and RowChanged
events.
Properties
The DataRow
class exposes several key properties for interacting with row data and state:
Item(int columnIndex)
Gets or sets the value of the specified column for this row, using the column's zero-based ordinal index.
Item(string columnName)
Gets or sets the value of the specified column for this row, using the column's name.
RowState
Gets the current state of the row (e.g., Added
, Modified
, Deleted
, Unchanged
).
Table
Gets the DataTable
to which this row belongs.
ItemArray
Gets or sets an array containing the values for all columns in this row.
HasErrors
Gets a value indicating whether any column in the row contains data errors.
Methods
Commonly used methods of the DataRow
class include:
AcceptChanges()
Accepts any changes made to the row since the last time AcceptChanges
was called. This resets the RowState
to Unchanged
.
BeginEdit()
Begins an edit operation on the row. Subsequent changes to the row are staged until EndEdit
or CancelEdit
is called.
CancelEdit()
Cancels the current edit operation on the row. Any changes made since BeginEdit
was called are discarded.
EndEdit()
Completes the edit operation on the row. If BeginEdit
was called, this method validates the changes and sets the RowState
appropriately.
RejectChanges()
Reverts all changes made to the row since the last time AcceptChanges
was called. This restores the row to its state prior to the changes.
SetAdded()
Sets the RowState
of the row to Added
. This is typically called internally by ADO.NET.
Delete()
Marks the row for deletion. The row is not physically removed from the collection until AcceptChanges
is called.
Events
DataRow
can raise events during data operations:
RowChanging
Fired when a change is made to the row (before the change is applied).
RowChanged
Fired after a change has been successfully made to the row.
RowErrorChanged
Fired when the RowError
property value changes.
Inheritance
The DataRow
class inherits from MarshalByRefObject
and implements ICloneable
.
System.Object
System.MarshalByRefObject
System.Data.DataRow
Examples
The following example demonstrates how to create a DataTable
, add a DataRow
, and access its values.
using System;
using System.Data;
public class Example
{
public static void Main()
{
// Create a new DataTable.
DataTable dataTable = new DataTable("Customers");
// Add columns to the DataTable.
DataColumn columnCustomerID = new DataColumn("CustomerID", typeof(int));
DataColumn columnCompanyName = new DataColumn("CompanyName", typeof(string));
DataColumn columnContactName = new DataColumn("ContactName", typeof(string));
dataTable.Columns.Add(columnCustomerID);
dataTable.Columns.Add(columnCompanyName);
dataTable.Columns.Add(columnContactName);
// Create a new DataRow using the DataTable's NewRow method.
DataRow newRow = dataTable.NewRow();
// Populate the DataRow with values.
newRow["CustomerID"] = 101;
newRow["CompanyName"] = "Awesome Corp";
newRow["ContactName"] = "Alice Wonderland";
// Add the DataRow to the DataTable.
dataTable.Rows.Add(newRow);
// Retrieve and display the data from the DataRow.
Console.WriteLine("Customer ID: {0}", newRow["CustomerID"]);
Console.WriteLine("Company Name: {0}", newRow["CompanyName"]);
Console.WriteLine("Contact Name: {0}", newRow["ContactName"]);
Console.WriteLine("Row State: {0}", newRow.RowState);
// Example of editing a row
Console.WriteLine("\nEditing row...");
newRow.BeginEdit();
newRow["ContactName"] = "Alice W.";
newRow.EndEdit();
Console.WriteLine("Updated Contact Name: {0}", newRow["ContactName"]);
Console.WriteLine("Row State after edit: {0}", newRow.RowState);
// Example of deleting a row
Console.WriteLine("\nDeleting row...");
newRow.Delete();
Console.WriteLine("Row State after delete: {0}", newRow.RowState);
}
}