RemoveAt Method

DataTableCollection.RemoveAt Method (Int32)


Removes the DataTable object at the specified index from the collection.

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

public void RemoveAt(
	int index


Type: System.Int32

The index of the DataTable to remove.

Exception Condition

The collection does not have a table at the specified index.

The CollectionChanged event occurs when a table is successfully removed.

The following example uses the Contains and CanRemove methods to test whether a table exists with the index 10. If so, the RemoveAt method is called to remove the table.

public static void DataTableCollectionRemoveAt()
    // Create a DataSet with two tables and then
    // remove the tables from the collection using RemoveAt.
    DataSet dataSet = new DataSet();

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

    // Create Orders table.
    DataTable ordersTable = new DataTable("Orders");
        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[] { customerTable, ordersTable });

    // Remove all tables.
    // First check to see if the table can be removed,
    // then remove it.
    // You 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))
        Console.WriteLine("dataSet has {0} tables",

.NET Framework
Available since 1.1
Return to top
© 2015 Microsoft