DataTableCollection Class
Represents the collection of tables for the DataSet.
For a list of all members of this type, see DataTableCollection Members.
System.Object
System.Data.InternalDataCollectionBase
System.Data.DataTableCollection
[Visual Basic] <Serializable> Public Class DataTableCollection Inherits InternalDataCollectionBase [C#] [Serializable] public class DataTableCollection : InternalDataCollectionBase [C++] [Serializable] public __gc class DataTableCollection : public InternalDataCollectionBase [JScript] public Serializable class DataTableCollection extends InternalDataCollectionBase
Thread Safety
This type is safe for multithreaded read operations. You must synchronize any write operations.
Remarks
The DataTableCollection contains all of the DataTable objects for a particular DataSet. To access the DataTableCollection of a DataSet, use the Tables property.
The DataTableCollection uses methods such as Add, Clear, and Remove to manage the items in the collection.
Use the Contains method to determine if a particular table (specified by either index or name) is in the collection.
To navigate from one table to another, use the ChildRelations or ParentRelations property of the DataTable to access its collection of DataRelation objects. You can also use the Relations property to navigate through the parent/child relationships of the DataTables in a given DataSet collection.
Example
[Visual Basic, C#, C++] The first example below retrieves the DataTableCollection of a DataSet and prints the value of each column, in each row, of each table. The second example creates a new DataTable with two columns, and adds it to the DataTableCollection.
[Visual Basic] Private Sub GetTables(ds As DataSet) ' Get Each DataTable in the DataTableCollection and print each row value. Dim t As DataTable Dim r As DataRow Dim c As DataColumn For Each t In ds.Tables For Each r In t.Rows For Each c in t.Columns If Not (r(c) Is Nothing) Then Console.WriteLine(r(c)) End If Next Next Next End Sub Private Sub CreateTable(ds As DataSet) Dim newTable As DataTable = new DataTable("MyTable") newTable.Columns.Add("ID", Type.GetType("System.Int32")) newTable.Columns.Add("Name", Type.GetType("System.String")) ds.Tables.Add(newTable) End Sub [C#] private void GetTables(DataSet ds) { // Get Each DataTable in the DataTableCollection and print each row value. foreach (DataTable t in ds.Tables) foreach (DataRow r in t.Rows) foreach (DataColumn c in t.Columns) if (r[c] != null) Console.WriteLine(r[c]); } private void CreateTable(DataSet ds) { DataTable newTable = new DataTable("MyTable"); newTable.Columns.Add("ID", typeof(int)); newTable.Columns.Add("Name", typeof(string)); ds.Tables.Add(newTable); } [C++] private: void GetTables(DataSet* ds) { // Get Each DataTable in the DataTableCollection and print each row value. System::Collections::IEnumerator* myEnum = ds->Tables->GetEnumerator(); while (myEnum->MoveNext()) { DataTable* t = __try_cast<DataTable*>(myEnum->Current); System::Collections::IEnumerator* myEnum1 = t->Rows->GetEnumerator(); while (myEnum1->MoveNext()) { DataRow* r = __try_cast<DataRow*>(myEnum1->Current); System::Collections::IEnumerator* myEnum2 = t->Columns->GetEnumerator(); while (myEnum2->MoveNext()) { DataColumn* c = __try_cast<DataColumn*>(myEnum2->Current); if (r->Item[c] != 0) Console::WriteLine(r->Item[c]); } } } } void CreateTable(DataSet* ds) { DataTable* newTable = new DataTable(S"MyTable"); newTable->Columns->Add(S"ID", __typeof(int)); newTable->Columns->Add(S"Name", __typeof(String)); ds->Tables->Add(newTable); }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Namespace: System.Data
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework
Assembly: System.Data (in System.Data.dll)
See Also
DataTableCollection Members | System.Data Namespace | DataColumn | DataRow | DataTable | DataSet