DataTable Class

Namespace

System.Data

Syntax

public sealed class DataTable : MarshalByValueObject, IListSource, IBindingList

Namespace: System.Data

Assembly: System.Data.dll

Description

Represents an in-memory collection of tables that provides a relational data structure. DataTable is one of the primary classes used to represent data in ADO.NET.

A DataTable can be used on its own to represent a single table of data, or it can be used with a DataSet to represent the tables in a relational database.

Key features of DataTable include:

  • Columns: Defines the schema of the table, including data types and constraints.
  • Rows: Stores the actual data for the table.
  • Constraints: Enforces data integrity through primary keys, unique constraints, and foreign keys.
  • Relations: Defines relationships between DataTable objects within a DataSet.
  • DataView: Allows for sorting, filtering, and searching of the data in the DataTable.

Properties

Name Description Access
CaseSensitive Gets or sets a value indicating whether string comparisons within this table are case-sensitive. Read/Write
ChildRelations Gets a collection of DataRelation objects that represent the child relations for this DataTable. Read
Columns Gets a DataColumnCollection that contains the columns for this DataTable. Read
Constraints Gets a ConstraintCollection that contains all constraints for this DataTable. Read
DataSet Gets the DataSet that contains this DataTable. Read
DefaultView Gets a DataView object that can be used to navigate and manipulate the data in the DataTable. Read
DisplayExpression Gets or sets the expression used to create a display string that identifies this table. Read/Write
HasErrors Gets a value indicating whether there are errors in any rows in this table. Read
IsInitialized Gets a value indicating whether the DataTable has been initialized. Read
Locale Gets or sets information about the current culture for this DataTable. Read/Write
MinimumCapacity Gets or sets the initial capacity of the DataTable. Read/Write
ParentRelations Gets a collection of DataRelation objects that represent the parent relations for this DataTable. Read
PrimaryKey Gets or sets the array of columns that constitute the primary key for this DataTable. Read/Write
RemotingContext Gets or sets the context that is used for remoting. Read/Write
Rows Gets a DataRowCollection that contains the rows for this DataTable. Read
Site Gets or sets the site of the component. Read/Write
TableName Gets or sets the name of this DataTable. Read/Write

Methods

Name Description
AcceptChanges()

Commits all changes that have been made to this DataTable since the last call to AcceptChanges().

public void AcceptChanges();
Commits all changes made to this DataTable.
BeginInit()

Begins a bulk update of the DataTable. Calls to BeginInit() must be matched by calls to EndInit().

public void BeginInit();
Begins a bulk update.
Clear()

Removes all rows and columns from the DataTable.

public void Clear();
Clears all rows and columns.
Clone()

Creates a new DataTable that is a copy of this DataTable. This method copies the schema, including the column definitions, constraints, and relations, but not the data.

public DataTable Clone();
Creates a copy of the schema.
Compute(string expression, string filter)

Evaluates the given expression by using DataColumn mapping and returns the computed value.

Parameters:
expression: The expression to evaluate.
filter: The filter to apply to the rows.

public object Compute(string expression, string filter);
Evaluates an expression.
Copy()

Creates a new DataTable that is a copy of this DataTable. This method copies the schema and the data.

public DataTable Copy();
Creates a copy of the table and its data.
CreateInstance()

Creates a DataTable that is a copy of this DataTable.

protected virtual object CreateInstance();
Creates an instance of the table.
Dispose()

Releases the unmanaged resources that are used by the current instance of the DataTable class and optionally releases the managed resources.

public void Dispose();
Releases resources.
EndInit()

Ends the bulk update of the DataTable.

public void EndInit();
Ends a bulk update.
Equals(object obj)

Determines whether the specified object is equal to the current object.

Parameters:
obj: The object to compare with the current object.

public override bool Equals(object obj);
Determines if two objects are equal.
Find(object primaryKey, ...)

Finds the row that has the specified primary key value. Assumes that the primary key has already been set.

Parameters:
primaryKey: The primary key value to search for.
...: Additional primary key values for composite keys.

public DataRow Find(object primaryKey);
Finds a row by its primary key.
GetChanges()

Returns a copy of the DataTable that contains all the changes made to it since it was loaded or the last time AcceptChanges() was called.

public DataTable GetChanges();
Returns a copy of the table with changes.
GetErrors()

Returns a DataRow array containing all the rows that have errors.

public DataRow[] GetErrors();
Returns rows with errors.
GetHashCode()

Serves as the default hash function.

public override int GetHashCode();
Serves as the default hash function.
GetType()

Gets the Type of the current instance.

public override Type GetType();
Gets the type of the current instance.
ImportRow(DataRow row)

Imports a DataRow into the DataTable.

Parameters:
row: The DataRow to import.

public void ImportRow(DataRow row);
Imports a data row.
Load(IDataReader reader, LoadOption loadOption, params DataTable[] tables)

Loads data from an IDataReader into the DataTable.

Parameters:
reader: The IDataReader to load data from.
loadOption: A LoadOption enumeration value that specifies how to handle existing data.
tables: An array of DataTable objects to load data into.

public void Load(IDataReader reader, LoadOption loadOption, params DataTable[] tables);
Loads data from an IDataReader.
Merge(DataTable table)

Merges the current DataTable with another DataTable.

Parameters:
table: The DataTable to merge with.

public void Merge(DataTable table);
Merges with another DataTable.
NewRow()

Creates a new DataRow with the same schema as this DataTable.

public DataRow NewRow();
Creates a new DataRow.
OnRowChanged(DataRowChangeEventArgs e)

Raises the RowChanged event.

Parameters:
e: A DataRowChangeEventArgs that contains the event data.

protected virtual void OnRowChanged(DataRowChangeEventArgs e);
Raises the RowChanged event.
OnRowChanging(DataRowChangeEventArgs e)

Raises the RowChanging event.

Parameters:
e: A DataRowChangeEventArgs that contains the event data.

protected virtual void OnRowChanging(DataRowChangeEventArgs e);
Raises the RowChanging event.
OnRowDeleted(DataRowChangeEventArgs e)

Raises the RowDeleted event.

Parameters:
e: A DataRowChangeEventArgs that contains the event data.

protected virtual void OnRowDeleted(DataRowChangeEventArgs e);
Raises the RowDeleted event.
OnRowDeleting(DataRowChangeEventArgs e)

Raises the RowDeleting event.

Parameters:
e: A DataRowChangeEventArgs that contains the event data.

protected virtual void OnRowDeleting(DataRowChangeEventArgs e);
Raises the RowDeleting event.
ReadXml(string fileName)

Reads XML data into the DataTable.

Parameters:
fileName: The name of the file to read.

public void ReadXml(string fileName);
Reads XML data from a file.
RejectChanges()

Rejects all changes that have been made to this DataTable since the last call to AcceptChanges().

public void RejectChanges();
Rejects all changes.
Reset()

Resets the DataTable to its initial state.

public void Reset();
Resets the DataTable.
Select(string filterExpression)

Returns an array of DataRow objects that match the specified filter expression.

Parameters:
filterExpression: The expression to filter rows by.

public DataRow[] Select(string filterExpression);
Selects rows based on a filter expression.
ToString()

Returns a string that represents the current object.

public override string ToString();
Returns a string representation.
WriteXml(string fileName)

Writes the DataTable to a file in XML format.

Parameters:
fileName: The name of the file to write to.

public void WriteXml(string fileName);
Writes XML data to a file.