DataTableCollection DataTableCollection DataTableCollection DataTableCollection Class

Definition

Represents the collection of tables for the DataSet.

public ref class DataTableCollection sealed : System::Data::InternalDataCollectionBase
[System.ComponentModel.ListBindable(false)]
[Serializable]
public sealed class DataTableCollection : System.Data.InternalDataCollectionBase
type DataTableCollection = class
    inherit InternalDataCollectionBase
Public NotInheritable Class DataTableCollection
Inherits InternalDataCollectionBase
Inheritance
Attributes
ListBindableAttribute SerializableAttribute

Examples

The first procedure in this example retrieves the DataTableCollection of a DataSet and prints the value of each column, in each row, of each table. The second procedure creates a new DataTable with two columns, and adds it to the DataTableCollection.

private void GetTables(DataSet dataSet)
{
    // Get Each DataTable in the DataTableCollection and 
    // print each row value.
    foreach (DataTable table in dataSet.Tables)
        foreach (DataRow row in table.Rows)
            foreach (DataColumn column in table.Columns)
                if (row[column] != null)
                    Console.WriteLine(row[column]);
}

private void CreateTable(DataSet dataSet)
{
    DataTable newTable = new DataTable("table");
    newTable.Columns.Add("ID", typeof(int));
    newTable.Columns.Add("Name", typeof(string));
    dataSet.Tables.Add(newTable);
}
Private Sub GetTables(dataSet As DataSet)
   ' Get Each DataTable in the DataTableCollection and 
   ' print each row value.
   Dim table As DataTable
   Dim row As DataRow
   Dim column As DataColumn
   For Each table In dataSet.Tables
      For Each row In table.Rows
         For Each column in table.Columns
            If Not (row(column) Is Nothing) Then
               Console.WriteLine(row(column))
            End If
         Next
      Next
   Next
End Sub

Private Sub CreateTable(dataSet As DataSet)
   Dim newTable As DataTable = new DataTable("table")
   newTable.Columns.Add("ID", Type.GetType("System.Int32"))
   newTable.Columns.Add("Name", Type.GetType("System.String"))
   dataSet.Tables.Add(newTable)
End Sub

Remarks

The DataTableCollection contains all 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 whether 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 particular DataSet collection.

Properties

Count Count Count Count

Gets the total number of elements in a collection.

(Inherited from InternalDataCollectionBase)
IsReadOnly IsReadOnly IsReadOnly IsReadOnly

Gets a value that indicates whether the InternalDataCollectionBase is read-only.

(Inherited from InternalDataCollectionBase)
IsSynchronized IsSynchronized IsSynchronized IsSynchronized

Gets a value that indicates whether the InternalDataCollectionBase is synchonized.

(Inherited from InternalDataCollectionBase)
Item[Int32] Item[Int32] Item[Int32] Item[Int32]

Gets the DataTable object at the specified index.

Item[String, String] Item[String, String] Item[String, String] Item[String, String]

Gets the DataTable object with the specified name in the specified namespace.

Item[String] Item[String] Item[String] Item[String]

Gets the DataTable object with the specified name.

List List List List

Gets the items of the collection as a list.

(Inherited from InternalDataCollectionBase)
SyncRoot SyncRoot SyncRoot SyncRoot

Gets an object that can be used to synchronize the collection.

(Inherited from InternalDataCollectionBase)

Methods

Add() Add() Add() Add()

Creates a new DataTable object by using a default name and adds it to the collection.

Add(DataTable) Add(DataTable) Add(DataTable) Add(DataTable)

Adds the specified DataTable to the collection.

Add(String) Add(String) Add(String) Add(String)

Creates a DataTable object by using the specified name and adds it to the collection.

Add(String, String) Add(String, String) Add(String, String) Add(String, String)

Creates a DataTable object by using the specified name and adds it to the collection.

AddRange(DataTable[]) AddRange(DataTable[]) AddRange(DataTable[]) AddRange(DataTable[])

Copies the elements of the specified DataTable array to the end of the collection.

CanRemove(DataTable) CanRemove(DataTable) CanRemove(DataTable) CanRemove(DataTable)

Verifies whether the specified DataTable object can be removed from the collection.

Clear() Clear() Clear() Clear()

Clears the collection of all DataTable objects.

Contains(String) Contains(String) Contains(String) Contains(String)

Gets a value that indicates whether a DataTable object with the specified name exists in the collection.

Contains(String, String) Contains(String, String) Contains(String, String) Contains(String, String)

Gets a value that indicates whether a DataTable object with the specified name and table namespace exists in the collection.

CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32)

Copies all the elements of the current InternalDataCollectionBase to a one-dimensional Array, starting at the specified InternalDataCollectionBase index.

(Inherited from InternalDataCollectionBase)
CopyTo(DataTable[], Int32) CopyTo(DataTable[], Int32) CopyTo(DataTable[], Int32) CopyTo(DataTable[], Int32)

Copies all the elements of the current DataTableCollection to a one-dimensional Array, starting at the specified destination array index.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

(Inherited from Object)
GetEnumerator() GetEnumerator() GetEnumerator() GetEnumerator()

Gets an IEnumerator for the collection.

(Inherited from InternalDataCollectionBase)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
IndexOf(DataTable) IndexOf(DataTable) IndexOf(DataTable) IndexOf(DataTable)

Gets the index of the specified DataTable object.

IndexOf(String) IndexOf(String) IndexOf(String) IndexOf(String)

Gets the index in the collection of the DataTable object with the specified name.

IndexOf(String, String) IndexOf(String, String) IndexOf(String, String) IndexOf(String, String)

Gets the index in the collection of the specified DataTable object.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
Remove(DataTable) Remove(DataTable) Remove(DataTable) Remove(DataTable)

Removes the specified DataTable object from the collection.

Remove(String) Remove(String) Remove(String) Remove(String)

Removes the DataTable object with the specified name from the collection.

Remove(String, String) Remove(String, String) Remove(String, String) Remove(String, String)

Removes the DataTable object with the specified name from the collection.

RemoveAt(Int32) RemoveAt(Int32) RemoveAt(Int32) RemoveAt(Int32)

Removes the DataTable object at the specified index from the collection.

ToString() ToString() ToString() ToString()

Returns a string that represents the current object.

(Inherited from Object)

Events

CollectionChanged CollectionChanged CollectionChanged CollectionChanged

Occurs after the DataTableCollection is changed because of DataTable objects being added or removed.

CollectionChanging CollectionChanging CollectionChanging CollectionChanging

Occurs while the DataTableCollection is changing because of DataTable objects being added or removed.

Extension Methods

Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable)

Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable)

Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable)

Enables parallelization of a query.

AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable)

Converts an IEnumerable to an IQueryable.

Applies to

Thread Safety

This type is safe for multithreaded read operations. You must synchronize any write operations.

See Also