This documentation is archived and is not being maintained.

DataTableCollection.RemoveAt Method

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

[Visual Basic]
Public Sub RemoveAt( _
   ByVal index As Integer _
)
[C#]
public void RemoveAt(
 int index
);
[C++]
public: void RemoveAt(
 int index
);
[JScript]
public function RemoveAt(
   index : int
);

Parameters

index
The index of the DataTable to remove.

Exceptions

Exception Type Condition
ArgumentException The collection does not have a table at the specified index.

Remarks

The OnCollectionChanged event occurs when a table is succesfully removed.

Example

[Visual Basic, C#, C++] The following example uses the Contains and CanRemove methods to test whether a table with the index 10 exists and can be removed. If so, the RemoveAt method is called to remove the table.

[Visual Basic] 
Public Shared Sub DataTableCollectionRemoveAt()
    ' Create a DataSet with two tables and then
    ' remove the tables from the collection using RemoveAt.
    Dim myDataSet As DataSet = New DataSet()

    ' Create Customer table.
    Dim t As DataTable = New DataTable("Customers")
    t.Columns.Add("customerId", GetType(Integer)).AutoIncrement = True
    t.Columns.Add("name", GetType(String))
    t.PrimaryKey = New DataColumn() {t.Columns("customerId")}

    ' Create Orders table.
    Dim t2 As DataTable = New DataTable("Orders")
    t2.Columns.Add("orderId", GetType(Integer)).AutoIncrement = True
    t2.Columns.Add("customerId", GetType(Integer))
    t2.Columns.Add("amount", GetType(Double))
    t2.PrimaryKey = New DataColumn() {t.Columns("orderId")}

    myDataSet.Tables.AddRange(New DataTable() {t, t2})

    ' 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.
    Do While (myDataSet.Tables.Count > 0)
        Dim table As DataTable = myDataSet.Tables(0)
        If (myDataSet.Tables.CanRemove(table)) Then
            myDataSet.Tables.RemoveAt(0)
        End If
        Console.WriteLine("myDataSet has {0} tables", myDataSet.Tables.Count)
    Loop
End Sub

[C#] 
public static void DataTableCollectionRemoveAt()
{
     // Create a DataSet with two tables and then
     // remove the tables from the collection using RemoveAt.
     DataSet myDataSet = new DataSet();

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

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

     myDataSet.Tables.AddRange(new DataTable[] {t, t2});

     // 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 (myDataSet.Tables.Count > 0)
     {
         DataTable table = myDataSet.Tables[0];
         if (myDataSet.Tables.CanRemove(table))
             myDataSet.Tables.RemoveAt(0);
         Console.WriteLine("myDataSet has {0} tables", myDataSet.Tables.Count);
     }
 }

[C++] 
public:
    static void DataTableCollectionRemoveAt()
    {
        // Create a DataSet with two tables and then
        // remove the tables from the collection using RemoveAt.
        DataSet* myDataSet = new DataSet();

        // Create Customer table.
        DataTable* t = new DataTable(S"Customers");
        t->Columns->Add(S"customerId", __typeof(int))->AutoIncrement = true;
        t->Columns->Add(S"name", __typeof(String));
        DataColumn* columnArray1[] = {t->Columns->Item[S"customerId"]};
        t->PrimaryKey = columnArray1;

        // Create Orders table.
        DataTable* t2 = new DataTable(S"Orders");
        t2->Columns->Add(S"orderId", __typeof(int))->AutoIncrement = true;
        t2->Columns->Add(S"customerId", __typeof(int));
        t2->Columns->Add(S"amount", __typeof(double));
        DataColumn* columnArray2[] = {t->Columns->Item[S"orderId"]};
        t2->PrimaryKey = columnArray2;

        DataTable* tableArray[] = {t, t2};
        myDataSet->Tables->AddRange(tableArray);

        // 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 (myDataSet->Tables->Count > 0) {
            DataTable* table = myDataSet->Tables->Item[0];
            if (myDataSet->Tables->CanRemove(table))
                myDataSet->Tables->RemoveAt(0);
            Console::WriteLine(S"myDataSet has {0} tables",__box( myDataSet->Tables->Count));
        }
    };

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

DataTableCollection Class | DataTableCollection Members | System.Data Namespace | IndexOf | Contains

Show: