Export (0) Print
Expand All

DataTableCollection.Remove Method (DataTable)

Removes the specified DataTable object from the collection.

Namespace:  System.Data
Assembly:  System.Data (in System.Data.dll)

'Declaration
Public Sub Remove ( _
	table As DataTable _
)

Parameters

table
Type: System.Data.DataTable

The DataTable to remove.

ExceptionCondition
ArgumentNullException

The value specified for the table is Nothing.

ArgumentException

The table does not belong to this collection.

-or-

The table is part of a relationship.

The CollectionChanged event occurs when a table is successfully removed.

To determine whether a given table exists and can be removed before invoking Remove, use the Contains and the CanRemove methods.

The following example uses the CanRemove method to test whether each table can be removed from a DataSet. If so, the Remove method is called to remove the table.

    Public Sub Main
	DataTableCollectionCanRemove
    End Sub 

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

        ' create Customer table 
        Dim customersTable As DataTable = 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 DataTable = 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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0
Show:
© 2015 Microsoft