Microsoft Docs

System.DataTable Class

Overview
Syntax
Properties
Methods
Events
Example

The System.DataTable class represents an in‑memory table of data. It can be used on its own or as part of a DataSet. A DataTable contains a collection of DataColumn, DataRow, and optional constraints.

Typical scenarios include reading data from a database, editing it offline, and then updating the source.

public class DataTable : MarshalByValueComponent, ISerializable, IListSource, ISupportInitialize, IDbDataAdapter, IXmlSerializable

Key Properties

PropertyTypeDescription
ColumnsDataColumnCollectionGets the collection of columns that belong to this table.
RowsDataRowCollectionGets the collection of rows that belong to this table.
PrimaryKeyDataColumn[]Gets or sets an array of columns that function as primary key for the table.
TableNamestringGets or sets the name of the table.
ConstraintsConstraintCollectionGets the collection of constraints (e.g., foreign keys) for the table.
NamespacestringGets or sets the namespace for the XML representation of the table.

Important Methods

MethodSignatureDescription
NewRowDataRow NewRow()Creates a new DataRow with the same schema as the table.
Rows.AddDataRow Add(DataRow row)Adds a DataRow to the table.
AcceptChangesvoid AcceptChanges()Commits all the changes made to the table since the last time AcceptChanges was called.
RejectChangesvoid RejectChanges()Rolls back all changes that have been made to the table since it was loaded or AcceptChanges was last called.
SelectDataRow[] Select(string filterExpression, string sort, DataViewRowState recordStates)Filters rows based on a criteria string.
Mergevoid Merge(DataTable table, bool preserveChanges, MissingSchemaAction missingSchemaAction)Merges the specified table with the current instance.
ReadXmlvoid ReadXml(string fileName)Populates the table from an XML file.
WriteXmlvoid WriteXml(string fileName)Writes the table's content to an XML file.

Relevant Events

EventDelegateDescription
RowChangedDataRowChangeEventHandlerOccurs after a row has been changed successfully.
RowChangingDataRowChangeEventHandlerOccurs before a row change is committed.
RowDeletedDataRowChangeEventHandlerOccurs after a row is deleted.
RowDeletingDataRowChangeEventHandlerOccurs before a row is deleted.
ColumnChangedDataColumnChangeEventHandlerOccurs after a column value is changed.
ColumnChangingDataColumnChangeEventHandlerOccurs before a column value is changed.

Example: Creating and Using a DataTable

using System;
using System.Data;

class Program
{
    static void Main()
    {
        // Create a new DataTable.
        DataTable table = new DataTable("Products");

        // Define columns.
        DataColumn idColumn = new DataColumn("ProductID", typeof(int));
        idColumn.AutoIncrement = true;
        idColumn.AutoIncrementSeed = 1;
        idColumn.Unique = true;

        DataColumn nameColumn = new DataColumn("ProductName", typeof(string));
        DataColumn priceColumn = new DataColumn("Price", typeof(decimal));

        table.Columns.Add(idColumn);
        table.Columns.Add(nameColumn);
        table.Columns.Add(priceColumn);

        // Set primary key.
        table.PrimaryKey = new DataColumn[] { idColumn };

        // Add rows.
        DataRow row = table.NewRow();
        row["ProductName"] = "Laptop";
        row["Price"] = 1199.99m;
        table.Rows.Add(row);

        row = table.NewRow();
        row["ProductName"] = "Smartphone";
        row["Price"] = 699.50m;
        table.Rows.Add(row);

        // Display data.
        foreach (DataRow r in table.Rows)
        {
            Console.WriteLine($"{r["ProductID"]}: {r["ProductName"]} - ${r["Price"]}");
        }

        // Save to XML.
        table.WriteXml("Products.xml");
    }
}