DataTableCollection.Add 方法

定义

DataTable 对象添加到集合中。Adds a DataTable object to the collection.

重载

Add()

使用默认名称创建一个新的 DataTable 对象,并将其添加到集合中。Creates a new DataTable object by using a default name and adds it to the collection.

Add(DataTable)

将指定的 DataTable 添加到集合。Adds the specified DataTable to the collection.

Add(String)

使用指定名称创建一个 DataTable 对象,并将其添加到集合中。Creates a DataTable object by using the specified name and adds it to the collection.

Add(String, String)

使用指定名称创建一个 DataTable 对象,并将其添加到集合中。Creates a DataTable object by using the specified name and adds it to the collection.

Add()

使用默认名称创建一个新的 DataTable 对象,并将其添加到集合中。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

返回

新创建的 DataTableThe newly created DataTable.

示例

下面的示例使用DataTable DataTableCollection不带参数的方法将三个新对象添加到中。AddThe 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

注解

由于未指定名称, DataTable因此将使用默认名称创建一个相对于其加法顺序的。Because no name is specified, the DataTable is created by using a default name, relative to its order of addition. 默认名称为 "Table1"。The default name is "Table1."

CollectionChanged表成功添加到集合时发生此事件。The CollectionChanged event occurs when a table is successfully added to the collection.

另请参阅

Add(DataTable)

将指定的 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)

参数

table
DataTable

要添加的 DataTable 对象。The DataTable object to add.

异常

为该表指定的值为 nullThe 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.

示例

下面的示例创建一个DataTable ,并将其添加DataTableCollectionDataSet的。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);
}

注解

CollectionChanged表成功添加到集合时发生此事件。The CollectionChanged event occurs when a table is successfully added to the collection.

另请参阅

Add(String)

使用指定名称创建一个 DataTable 对象,并将其添加到集合中。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

参数

name
String

要赋予已创建的 DataTable 的名称。The name to give the created DataTable.

返回

新创建的 DataTableThe newly created DataTable.

异常

集合中的某个表具有相同的名称。A table in the collection has the same name. (该比较不区分大小写。)(The comparison is not case sensitive.)

示例

下面的示例向添加DataTable一个具有给定名称DataTableCollection的。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

注解

如果传入了DataTable或空字符串(""),则将为新创建的指定null一个默认名称。If either null or an empty string ("") is passed in, a default name is given to the newly created DataTable. 此名称基于添加表的顺序("Table1"、"Table2" 等)。This name is based on the order in which the table was added ("Table1", "Table2", and so on).

如果CollectionChanged表已成功添加到集合中,则会发生该事件。The CollectionChanged event occurs if the table is successfully added to the collection.

另请参阅

Add(String, String)

使用指定名称创建一个 DataTable 对象,并将其添加到集合中。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

参数

name
String

要赋予已创建的 DataTable 的名称。The name to give the created DataTable.

tableNamespace
String

要为已创建的 DataTable 提供的命名空间。The namespace to give the created DataTable.

返回

新创建的 DataTableThe newly created DataTable.

异常

集合中的某个表具有相同的名称。A table in the collection has the same name. (该比较不区分大小写。)(The comparison is not case sensitive.)

示例

下面的示例向添加DataTable一个具有给定名称DataTableCollection的。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

注解

如果传入了DataTable或空字符串(""),则将为新创建的指定null一个默认名称。If either null or an empty string ("") is passed in, a default name is given to the newly created DataTable. 此名称基于添加表的顺序("Table1"、"Table2" 等)。This name is based on the order in which the table was added ("Table1", "Table2", and so on).

如果CollectionChanged表已成功添加到集合中,则会发生该事件。The CollectionChanged event occurs if the table is successfully added to the collection.

另请参阅

适用于