Export (0) Print
Expand All

DataTableCollection.Add Method

Adds a DataTable object to the collection.

Overload List

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function Add() As DataTable
[C#] public virtual DataTable Add();
[C++] public: virtual DataTable* Add();
[JScript] public function Add() : DataTable;

Adds the specified DataTable to the collection.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Sub Add(DataTable)
[C#] public virtual void Add(DataTable);
[C++] public: virtual void Add(DataTable*);
[JScript] public function Add(DataTable);

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function Add(String) As DataTable
[C#] public virtual DataTable Add(string);
[C++] public: virtual DataTable* Add(String*);
[JScript] public function Add(String) : DataTable;

Example

[Visual Basic, C#, C++] The following example adds a DataTable with the given name to the DataTableCollection.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of Add. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Private Sub AddTable()
   Dim dSet As DataSet
   Dim dt As DataTable
   ' Presuming a DataGrid is displaying more than one table, get its DataSet.
   dSet = CType(DataGrid1.DataSource, DataSet)
   ' Use the Add method to add a new table with a given name.
   dt = dSet.Tables.Add("myNewTable")
   ' Code to add columns and rows not shown here.
   MessageBox.Show(dt.TableName)
   MessageBox.Show(dSet.Tables.Count.ToString())
End Sub

[C#] 
private void AddTable()
{
   DataSet dSet;
   DataTable dt;
   // Presuming a DataGrid is displaying more than one table, get its DataSet.
   dSet = (DataSet)DataGrid1.DataSource;
   // Use the Add method to add a new table with a given name.
   dt = dSet.Tables.Add("myNewTable");
   // Code to add columns and rows not shown here.
   MessageBox.Show(dt.TableName);
   MessageBox.Show(dSet.Tables.Count.ToString());
}

[C++] 
private:
 void AddTable()
 {
    DataSet* dSet;
    DataTable* dt;
    // Presuming a DataGrid is displaying more than one table, get its DataSet.
    dSet = dynamic_cast<DataSet*>(DataGrid1->DataSource);
    // Use the Add method to add a new table with a given name.
    dt = dSet->Tables->Add(S"myNewTable");
    // Code to add columns and rows not shown here.
    MessageBox::Show(dt->TableName);
    MessageBox::Show(dSet->Tables->Count.ToString());
 }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

DataTableCollection Class | DataTableCollection Members | System.Data Namespace

Show:
© 2014 Microsoft