Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Remove Method (DataTable)
Collapse the table of content
Expand the table of content

DataTableCollection.Remove Method (DataTable)

Removes the specified DataTable object from the collection.

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

Public Sub Remove ( _
	table As DataTable _


Type: System.Data.DataTable

The DataTable to remove.


The value specified for the table is Nothing.


The table does not belong to this collection.


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
    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", _
        customersTable.PrimaryKey = New DataColumn() _

        ' 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", _
        ordersTable.Columns.Add("amount", _
        ordersTable.PrimaryKey = New DataColumn() _

        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
            End If 

	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
© 2015 Microsoft