Microsoft Docs

DataRow Class

Namespace: System.Data

Assembly: System.Data.dll

Overview
Syntax
Properties
Methods
Remarks
Examples

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
PropertyTypeDescription
ItemobjectGets or sets the value of a column in the row using column name or index.
TableDataTableGets the DataTable that contains the row.
RowStateDataRowStateGets the state of the row (Added, Modified, Deleted, etc.).
HasVersionboolIndicates whether the row has a version of data for the specified DataRowVersion.
IsNullboolChecks if a column value is null.
MethodSignatureDescription
BeginEditvoid BeginEdit()Begins an edit operation on the row.
CancelEditvoid CancelEdit()Cancels the edit operation.
EndEditvoid EndEdit()Ends the edit operation and commits changes.
Deletevoid Delete()Deletes the row from its table.
AcceptChangesvoid AcceptChanges()Commits all the changes made to the row.
RejectChangesvoid RejectChanges()Reverts the row to its original state.
SetAddedvoid SetAdded()Marks the row as added.
SetModifiedvoid 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}");
    }
}