次の方法で共有


DataTableCollection.RemoveAt メソッド

指定したインデックス位置にある DataTable オブジェクトをコレクションから削除します。

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

パラメータ

  • index
    削除する DataTable のインデックス。

例外

例外の種類 条件
ArgumentException このコレクションには指定したインデックス位置にテーブルがありません。

解説

テーブルが正常に削除されると、 OnCollectionChanged イベントが発生します。

使用例

[Visual Basic, C#, C++] Contains メソッドと CanRemove メソッドを使用して、インデックス 10 のテーブルがあるかどうか、および削除できるかどうかを確認する例を次に示します。削除できる場合は、 RemoveAt メソッドが呼び出されてテーブルが削除されます。

 
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] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

DataTableCollection クラス | DataTableCollection メンバ | System.Data 名前空間 | IndexOf | Contains