DataTableCollection.Add Method

Definition

Adds a DataTable object to the collection.

Overloads

Add()

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

Add(DataTable)

Adds the specified DataTable to the collection.

Add(String)

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

Add(String, String)

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

Add()

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

public:
 virtual System::Data::DataTable ^ Add();
public virtual System.Data.DataTable Add ();
abstract member Add : unit -> System.Data.DataTable
override this.Add : unit -> System.Data.DataTable
Public Overridable Function Add () As DataTable

Returns

The newly created DataTable.

Examples

The following example adds three new DataTable objects to the DataTableCollection using the Add method without arguments.

private void AddTables()
{
    // Presuming a DataGrid is displaying more than one table, 
    // get its DataSet.
    DataSet thisDataSet = (DataSet)DataGrid1.DataSource;

    for (int i = 0; i < 3; i++)
        thisDataSet.Tables.Add();
    Console.WriteLine(thisDataSet.Tables.Count.ToString() 
        + " tables");
    foreach (DataTable table in thisDataSet.Tables)
        Console.WriteLine(table.TableName);
}
Private Sub AddTables()
   Dim table As DataTable
   
   ' Presuming a DataGrid is displaying more than one table, get its DataSet.
   Dim thisDataSet As DataSet = CType(DataGrid1.DataSource, DataSet)
   Dim i As Integer
   For i = 0 to 2
      thisDataSet.Tables.Add()
   Next i

   Console.WriteLine(thisDataSet.Tables.Count.ToString() & " tables")
   For Each table In thisDataSet.Tables
      Console.WriteLine(table.TableName)
   Next
End Sub

Remarks

Because no name is specified, the DataTable is created by using a default name, relative to its order of addition. The default name is "Table1."

The CollectionChanged event occurs when a table is successfully added to the collection.

See also

Add(DataTable)

Adds the specified DataTable to the collection.

public:
 virtual void Add(System::Data::DataTable ^ table);
public virtual void Add (System.Data.DataTable table);
abstract member Add : System.Data.DataTable -> unit
override this.Add : System.Data.DataTable -> unit
Public Overridable Sub Add (table As DataTable)

Parameters

table
DataTable

The DataTable object to add.

Exceptions

The value specified for the table is null.

The table already belongs to this collection, or belongs to another collection.

A table in the collection has the same name. The comparison is not case sensitive.

Examples

The following example creates a DataTable and adds it to the DataTableCollection of a DataSet.

private void AddDataTable()
{
    // Get the DataTableCollection of a DataGrid 
    // control's DataSet.
    DataTableCollection tables = 
        ((DataSet)DataGrid1.DataSource).Tables;

    // Create a new DataTable.
    DataTable table = new DataTable();

    // Code to add columns and rows not shown here.

    // Add the table to the DataTableCollection.
    tables.Add(table);
}

Remarks

The CollectionChanged event occurs when a table is successfully added to the collection.

See also

Add(String)

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

public:
 virtual System::Data::DataTable ^ Add(System::String ^ name);
public virtual System.Data.DataTable Add (string name);
abstract member Add : string -> System.Data.DataTable
override this.Add : string -> System.Data.DataTable
Public Overridable Function Add (name As String) As DataTable

Parameters

name
String

The name to give the created DataTable.

Returns

The newly created DataTable.

Exceptions

A table in the collection has the same name. (The comparison is not case sensitive.)

Examples

The following example adds a DataTable with the given name to the DataTableCollection.

private void AddTable()
{
    // Presuming a DataGrid is displaying more than one table, 
    // get its DataSet.
    DataSet thisDataSet = (DataSet)DataGrid1.DataSource;

    // Use the Add method to add a new table with a given name.
    DataTable table = thisDataSet.Tables.Add("NewTable");

    // Code to add columns and rows not shown here.

    Console.WriteLine(table.TableName);
    Console.WriteLine(thisDataSet.Tables.Count.ToString());
}
Private Sub AddTable()
    ' Presuming a DataGrid is displaying more than one table, 
    ' get its DataSet.
    Dim thisDataSet As DataSet = _
        CType(DataGrid1.DataSource, DataSet)

    ' Use the Add method to add a new table with a given name.
    Dim table As DataTable = thisDataSet.Tables.Add("NewTable")

    ' Code to add columns and rows not shown here.

    Console.WriteLine(table.TableName)
    Console.WriteLine(thisDataSet.Tables.Count.ToString())
End Sub

Remarks

If either null or an empty string ("") is passed in, a default name is given to the newly created DataTable. This name is based on the order in which the table was added ("Table1", "Table2", and so on).

The CollectionChanged event occurs if the table is successfully added to the collection.

See also

Add(String, String)

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

public:
 System::Data::DataTable ^ Add(System::String ^ name, System::String ^ tableNamespace);
public System.Data.DataTable Add (string name, string tableNamespace);
member this.Add : string * string -> System.Data.DataTable
Public Function Add (name As String, tableNamespace As String) As DataTable

Parameters

name
String

The name to give the created DataTable.

tableNamespace
String

The namespace to give the created DataTable.

Returns

The newly created DataTable.

Exceptions

A table in the collection has the same name. (The comparison is not case sensitive.)

Examples

The following example adds a DataTable with the given name to the DataTableCollection.

private void AddTable()
{
    // Presuming a DataGrid is displaying more than one table, 
    // get its DataSet.
    DataSet thisDataSet = (DataSet)DataGrid1.DataSource;

    // Use the Add method to add a new table with a given name.
    DataTable table = thisDataSet.Tables.Add("NewTable");

    // Code to add columns and rows not shown here.

    Console.WriteLine(table.TableName);
    Console.WriteLine(thisDataSet.Tables.Count.ToString());
}
Private Sub AddTable()
    ' Presuming a DataGrid is displaying more than one table, 
    ' get its DataSet.
    Dim thisDataSet As DataSet = _
        CType(DataGrid1.DataSource, DataSet)

    ' Use the Add method to add a new table with a given name.
    Dim table As DataTable = thisDataSet.Tables.Add("NewTable")

    ' Code to add columns and rows not shown here.

    Console.WriteLine(table.TableName)
    Console.WriteLine(thisDataSet.Tables.Count.ToString())
End Sub

Remarks

If either null or an empty string ("") is passed in, a default name is given to the newly created DataTable. This name is based on the order in which the table was added ("Table1", "Table2", and so on).

The CollectionChanged event occurs if the table is successfully added to the collection.

See also

Applies to