DataTableCollection.RemoveAt 方法

从集合中移除位于指定索引位置的 DataTable 对象。

**命名空间:**System.Data
**程序集:**System.Data(在 system.data.dll 中)

语法

声明
Public Sub RemoveAt ( _
    index As Integer _
)
用法
Dim instance As DataTableCollection
Dim index As Integer

instance.RemoveAt(index)
public void RemoveAt (
    int index
)
public:
void RemoveAt (
    int index
)
public void RemoveAt (
    int index
)
public function RemoveAt (
    index : int
)

参数

  • index
    要移除的 DataTable 的索引。

异常

异常类型 条件

ArgumentException

该集合在指定的索引位置没有表。

备注

当成功移除表时,将发生 CollectionChanged 事件。

示例

下面的示例使用 ContainsCanRemove 方法测试是否存在索引为 10 的表。如果存在,则调用 RemoveAt 方法移除该表。

Public Sub DataTableCollectionRemoveAt()
    ' Create a DataSet with two tables and then
    ' remove the tables from the collection using RemoveAt.
    Dim dataSet As DataSet = New DataSet()

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

    ' Create Orders table.
    Dim ordersTable As DataTable = New DataTable("Orders")
    ordersTable.Columns.Add("orderId", _
        GetType(Integer)).AutoIncrement = True
    ordersTable.Columns.Add("customerId", GetType(Integer))
    ordersTable.Columns.Add("amount", GetType(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.
    Do While (dataSet.Tables.Count > 0)
        Dim table As DataTable = dataSet.Tables(0)
        If (dataSet.Tables.CanRemove(table)) Then
            dataSet.Tables.RemoveAt(0)
        End If
        Console.WriteLine("dataSet has {0} tables", _
            dataSet.Tables.Count)
    Loop
End Sub
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");
    customerTable.Columns.Add("customerId",
        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");
    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[] { 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))
            dataSet.Tables.RemoveAt(0);
        Console.WriteLine("dataSet has {0} tables",
            dataSet.Tables.Count);
    }
}

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

DataTableCollection 类
DataTableCollection 成员
System.Data 命名空间
IndexOf
Contains