次の方法で共有


DataTableCollection.Contains メソッド

指定した名前の DataTable オブジェクトがコレクション内に存在するかどうかを示す値を取得します。

Public Function Contains( _
   ByVal name As String _) As Boolean
[C#]
public bool Contains(stringname);
[C++]
public: bool Contains(String* name);
[JScript]
public function Contains(
   name : String) : Boolean;

パラメータ

  • name
    検索する DataTable の名前。

戻り値

指定したテーブルが存在する場合は true 。それ以外の場合は false

解説

TableName プロパティを使用して DataTable オブジェクトの名前を指定します。引数を渡さずに Add メソッドを使用して DataTable をコレクションに追加した場合は、テーブルの追加順序に基づいて既定の名前がテーブルに付けられます (Table1、Table2 など)。

DataTable のインデックスを取得するには、 IndexOf メソッドを使用します。

使用例

[Visual Basic, C#, C++] "Suppliers" という名前のテーブルが DataTableCollection コレクション内に存在するかどうかを確認する例を次に示します。

 
Private Sub TestForTableName()
   Dim dSet As DataSet
   ' Get the DataSet of a DataGrid.
   dSet = CType(DataGrid1.DataSource, DataSet)
   ' Get the DataTableCollection through the Tables property.
   Dim tablesCol As DataTableCollection
   tablesCol = dSet.Tables
   ' Check if the named table exists.
   If tablesCol.Contains("Suppliiers") Then 
      MessageBox.Show("Table named Suppliers exists")
   End If
End Sub

[C#] 
private void TestForTableName()
{
   DataSet dSet;
   // Get the DataSet of a DataGrid.
   dSet = (DataSet)DataGrid1.DataSource;
   // Get the DataTableCollection through the Tables property.
   DataTableCollection tablesCol = dSet.Tables;
   // Check if the named table exists.
   if (tablesCol.Contains("Suppliiers")) 
      MessageBox.Show("Table named Suppliers exists");
}

[C++] 
private:
 void TestForTableName()
 {
    DataSet* dSet;
    // Get the DataSet of a DataGrid.
    dSet = dynamic_cast<DataSet*>(DataGrid1->DataSource);
    // Get the DataTableCollection through the Tables property.
    DataTableCollection* tablesCol = dSet->Tables;
    // Check if the named table exists.
    if (tablesCol->Contains(S"Suppliiers")) 
       MessageBox::Show(S"Table named Suppliers exists");
 }

[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 名前空間 | Item | IndexOf | Remove | CanRemove