次の方法で共有


DataTableCollection.Remove メソッド (DataTable)

指定した DataTable オブジェクトをコレクションから削除します。

Overloads Public Sub Remove( _
   ByVal table As DataTable _)
[C#]
public void Remove(DataTabletable);
[C++]
public: void Remove(DataTable* table);
[JScript]
public function Remove(
   table : DataTable);

パラメータ

  • table
    削除する DataTable

例外

例外の種類 条件
ArgumentNullException このテーブルに指定した値が null 参照 (Visual Basic では Nothing) です。
ArgumentException このテーブルがこのコレクションに属していません。

または

このテーブルはリレーションシップの一部です。

解説

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

Remove を呼び出す前にそのテーブルが存在するかどうか、および削除できるかどうかを確認するには、 Contains メソッドと CanRemove メソッドを使用します。

使用例

[Visual Basic, C#, C++] CanRemove メソッドを使用して、各テーブルを DataSet から削除できるかどうかを確認する例を次に示します。削除できる場合は、テーブルを削除するために Remove メソッドが呼び出されます。

 
[C#] 
public static void DataTableCollectionCanRemove()
{
    // create a DataSet with two tables
    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
    // check if table can be removed and then
    // remove it, 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.Remove( table );
        }
    }

    Console.WriteLine( "myDataSet has {0} tables", myDataSet.Tables.Count );
}

[C++] 
public:
    static void DataTableCollectionCanRemove()
    {
        // create a DataSet with two tables
        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* temp1 [] = {t->Columns->Item[S"customerId"]};
        t->PrimaryKey = temp1;

        // 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* temp2 [] = {t->Columns->Item[S"orderId"]};
        t2->PrimaryKey = temp2;

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

        // remove all tables
        // check if table can be removed and then
        // remove it, 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->Remove( table );
            }
        }

        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 名前空間 | DataTableCollection.Remove オーバーロードの一覧 | IndexOf | Contains