DataTableCollection.Remove 方法

定义

从集合中删除指定的 DataTable 对象。Removes a specified DataTable object from the collection.

重载

Remove(String, String)

从集合中删除具有指定名称的 DataTable 对象。Removes the DataTable object with the specified name from the collection.

Remove(DataTable)

从集合中移除指定的 DataTable 对象。Removes the specified DataTable object from the collection.

Remove(String)

从集合中删除具有指定名称的 DataTable 对象。Removes the DataTable object with the specified name from the collection.

Remove(String, String)

从集合中删除具有指定名称的 DataTable 对象。Removes the DataTable object with the specified name from the collection.

public:
 void Remove(System::String ^ name, System::String ^ tableNamespace);
public void Remove (string name, string tableNamespace);
member this.Remove : string * string -> unit
Public Sub Remove (name As String, tableNamespace As String)

参数

name
String

要删除的 DataTable 对象的名称。The name of the DataTable object to remove.

tableNamespace
String

作为查找范围的 DataTable 命名空间的名称。The name of the DataTable namespace to look in.

异常

该集合不包含具有指定名称的表。The collection does not have a table with the specified name.

示例

下面的示例使用 ContainsCanRemove 方法来测试指定的表是否存在并且是否可以删除。The following example uses the Contains and CanRemove methods to test whether a named table exists and can be removed. 如果是这样,则调用 Remove 方法以删除表。If so, the Remove method is called to remove the table.

private void RemoveTables()
{
    // Set the name of the table to test for and remove.
    string name = "Suppliers";

    // Presuming a DataGrid is displaying more than one table, get its DataSet.
    DataSet thisDataSet = (DataSet)DataGrid1.DataSource;
    DataTableCollection tablesCol = thisDataSet.Tables;
    if (tablesCol.Contains(name) && tablesCol.CanRemove(tablesCol[name])) 
        tablesCol.Remove(name);
}
Private Sub RemoveTables()
   ' Set the name of the table to test for and remove.
   Dim name As String = "Suppliers"

   ' Presuming a DataGrid is displaying more than one table, get its DataSet.
   Dim thisDataSet As DataSet = CType(DataGrid1.DataSource, DataSet)
   Dim tablesCol As DataTableCollection = thisDataSet.Tables
   If tablesCol.Contains(name) _
   And tablesCol.CanRemove(tablesCol(name)) Then 
      tablesCol.Remove(name)
   End If
End Sub

注解

成功删除表时,将发生 CollectionChanged 事件。The CollectionChanged event occurs when a table is successfully removed.

若要确定给定的表是否存在,以及是否可以在调用 Remove之前将其移除,请使用 ContainsCanRemove 方法。To determine whether a given table exists and can be removed before invoking Remove, use the Contains and the CanRemove methods.

另请参阅

Remove(DataTable)

从集合中移除指定的 DataTable 对象。Removes the specified DataTable object from the collection.

public:
 void Remove(System::Data::DataTable ^ table);
public void Remove (System.Data.DataTable table);
member this.Remove : System.Data.DataTable -> unit
Public Sub Remove (table As DataTable)

参数

table
DataTable

要移除的 DataTableThe DataTable to remove.

异常

为该表指定的值为 nullThe value specified for the table is null.

该表不属于此集合。The table does not belong to this collection.

- 或 --or-

该表是关系的一部分。The table is part of a relationship.

示例

下面的示例使用 CanRemove 方法来测试是否可以从 DataSet中删除每个表。The following example uses the CanRemove method to test whether each table can be removed from a DataSet. 如果是这样,则调用 Remove 方法以删除表。If so, the Remove method is called to remove the table.

public static void DataTableCollectionCanRemove()
{
    // create a DataSet with two tables
    DataSet dataSet = new DataSet();

    // create Customer table
    DataTable customersTable = new DataTable("Customers");
    customersTable.Columns.Add("customerId", 
        typeof(int) ).AutoIncrement = true;
    customersTable.Columns.Add("name",       
        typeof(string));
    customersTable.PrimaryKey = new DataColumn[] 
        { customersTable.Columns["customerId"] };

    // create Orders table
    DataTable ordersTable = new DataTable("Orders");
    ordersTable.Columns.Add("orderId",    
        typeof(int) ).AutoIncrement = true;
    ordersTable.Columns.Add("customerId", 
        typeof(int) );
    ordersTable.Columns.Add("amount",     
        typeof(double));
    ordersTable.PrimaryKey = new DataColumn[] 
        { ordersTable.Columns["orderId"] };

    dataSet.Tables.AddRange(new DataTable[] 
        {customersTable, ordersTable });

    // remove all tables
    // check if table can be removed and then
    // remove it, cannot use a foreach when
    // removing items from a collection
    while(dataSet.Tables.Count > 0)
    {
        DataTable table = dataSet.Tables[0];
        if(dataSet.Tables.CanRemove(table))
        {
            dataSet.Tables.Remove(table);
        }
    }

    Console.WriteLine("dataSet has {0} tables",     
        dataSet.Tables.Count);
}
Public Sub Main
DataTableCollectionCanRemove
End Sub

Public Sub DataTableCollectionCanRemove()
    ' create a DataSet with two tables
    Dim dataSet As New DataSet()

    ' create Customer table
    Dim customersTable As New DataTable("Customers")
    customersTable.Columns.Add("customerId", _
        System.Type.GetType("System.Integer")).AutoIncrement = True
    customersTable.Columns.Add("name", _
        System.Type.GetType("System.String"))
    customersTable.PrimaryKey = New DataColumn() _
        {customersTable.Columns("customerId")}

    ' create Orders table
    Dim ordersTable As New DataTable("Orders")
    ordersTable.Columns.Add("orderId", _
        System.Type.GetType("System.Integer")).AutoIncrement = True
    ordersTable.Columns.Add("customerId", _
        System.Type.GetType("System.Integer"))
    ordersTable.Columns.Add("amount", _
        System.Type.GetType("System.Double"))
    ordersTable.PrimaryKey = New DataColumn() _
        {ordersTable.Columns("orderId")}

    dataSet.Tables.AddRange(New DataTable() {customersTable, ordersTable})

    ' remove all tables
    ' check if table can be removed and then
    ' remove it, cannot use a foreach when
    ' removing items from a collection
    Do While (dataSet.Tables.Count > 0)
        Dim table As DataTable = dataSet.Tables(0)
        If (dataSet.Tables.CanRemove(table)) Then
            dataSet.Tables.Remove(table)
        End If
    Loop

Console.WriteLine("dataSet has {0} tables", dataSet.Tables.Count)
End Sub

注解

成功删除表时,将发生 CollectionChanged 事件。The CollectionChanged event occurs when a table is successfully removed.

若要确定给定的表是否存在,以及是否可以在调用 Remove之前将其移除,请使用 ContainsCanRemove 方法。To determine whether a given table exists and can be removed before invoking Remove, use the Contains and the CanRemove methods.

另请参阅

Remove(String)

从集合中删除具有指定名称的 DataTable 对象。Removes the DataTable object with the specified name from the collection.

public:
 void Remove(System::String ^ name);
public void Remove (string name);
member this.Remove : string -> unit
Public Sub Remove (name As String)

参数

name
String

要删除的 DataTable 对象的名称。The name of the DataTable object to remove.

异常

该集合不包含具有指定名称的表。The collection does not have a table with the specified name.

示例

下面的示例使用 ContainsCanRemove 方法来测试指定的表是否存在并且是否可以删除。The following example uses the Contains and CanRemove methods to test whether a named table exists and can be removed. 如果是这样,则调用 Remove 方法以删除表。If so, the Remove method is called to remove the table.

private void RemoveTables()
{
    // Set the name of the table to test for and remove.
    string name = "Suppliers";

    // Presuming a DataGrid is displaying more than one table, get its DataSet.
    DataSet thisDataSet = (DataSet)DataGrid1.DataSource;
    DataTableCollection tablesCol = thisDataSet.Tables;
    if (tablesCol.Contains(name) && tablesCol.CanRemove(tablesCol[name])) 
        tablesCol.Remove(name);
}
Private Sub RemoveTables()
   ' Set the name of the table to test for and remove.
   Dim name As String = "Suppliers"

   ' Presuming a DataGrid is displaying more than one table, get its DataSet.
   Dim thisDataSet As DataSet = CType(DataGrid1.DataSource, DataSet)
   Dim tablesCol As DataTableCollection = thisDataSet.Tables
   If tablesCol.Contains(name) _
   And tablesCol.CanRemove(tablesCol(name)) Then 
      tablesCol.Remove(name)
   End If
End Sub

注解

成功删除表时,将发生 CollectionChanged 事件。The CollectionChanged event occurs when a table is successfully removed.

若要确定给定的表是否存在,以及是否可以在调用 Remove之前将其移除,请使用 ContainsCanRemove 方法。To determine whether a given table exists and can be removed before invoking Remove, use the Contains and the CanRemove methods.

另请参阅

适用于