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
| Property | Type | Description |
|---|---|---|
Columns | DataColumnCollection | Gets the collection of columns that belong to this table. |
Rows | DataRowCollection | Gets the collection of rows that belong to this table. |
PrimaryKey | DataColumn[] | Gets or sets an array of columns that function as primary key for the table. |
TableName | string | Gets or sets the name of the table. |
Constraints | ConstraintCollection | Gets the collection of constraints (e.g., foreign keys) for the table. |
Namespace | string | Gets or sets the namespace for the XML representation of the table. |
Important Methods
| Method | Signature | Description |
|---|---|---|
NewRow | DataRow NewRow() | Creates a new DataRow with the same schema as the table. |
Rows.Add | DataRow Add(DataRow row) | Adds a DataRow to the table. |
AcceptChanges | void AcceptChanges() | Commits all the changes made to the table since the last time AcceptChanges was called. |
RejectChanges | void RejectChanges() | Rolls back all changes that have been made to the table since it was loaded or AcceptChanges was last called. |
Select | DataRow[] Select(string filterExpression, string sort, DataViewRowState recordStates) | Filters rows based on a criteria string. |
Merge | void Merge(DataTable table, bool preserveChanges, MissingSchemaAction missingSchemaAction) | Merges the specified table with the current instance. |
ReadXml | void ReadXml(string fileName) | Populates the table from an XML file. |
WriteXml | void WriteXml(string fileName) | Writes the table's content to an XML file. |
Relevant Events
| Event | Delegate | Description |
|---|---|---|
RowChanged | DataRowChangeEventHandler | Occurs after a row has been changed successfully. |
RowChanging | DataRowChangeEventHandler | Occurs before a row change is committed. |
RowDeleted | DataRowChangeEventHandler | Occurs after a row is deleted. |
RowDeleting | DataRowChangeEventHandler | Occurs before a row is deleted. |
ColumnChanged | DataColumnChangeEventHandler | Occurs after a column value is changed. |
ColumnChanging | DataColumnChangeEventHandler | Occurs 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");
}
}