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()

Source:
DataTableCollection.cs
Source:
DataTableCollection.cs
Source:
DataTableCollection.cs

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

C#
public System.Data.DataTable Add();
C#
public virtual System.Data.DataTable Add();

Returns

The newly created DataTable.

Examples

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

C#
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);
}

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

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Add(DataTable)

Source:
DataTableCollection.cs
Source:
DataTableCollection.cs
Source:
DataTableCollection.cs

Adds the specified DataTable to the collection.

C#
public void Add(System.Data.DataTable table);
C#
public virtual void Add(System.Data.DataTable table);

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.

C#
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

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Add(String)

Source:
DataTableCollection.cs
Source:
DataTableCollection.cs
Source:
DataTableCollection.cs

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

C#
public System.Data.DataTable Add(string? name);
C#
public System.Data.DataTable Add(string name);
C#
public virtual System.Data.DataTable Add(string name);

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.

C#
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());
}

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

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Add(String, String)

Source:
DataTableCollection.cs
Source:
DataTableCollection.cs
Source:
DataTableCollection.cs

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

C#
public System.Data.DataTable Add(string? name, string? tableNamespace);
C#
public System.Data.DataTable Add(string name, string tableNamespace);

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.

C#
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());
}

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

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1