Export (0) Print
Expand All

DataTableCollection.Contains Method (String)

Gets a value that indicates whether a DataTable object with the specified name exists in the collection.

Namespace:  System.Data
Assembly:  System.Data (in System.Data.dll)

public bool Contains(
	string name
)

Parameters

name
Type: System.String

The name of the DataTable to find.

Return Value

Type: System.Boolean
true if the specified table exists; otherwise false.

You specify the name of the DataTable object by using the TableName property. If you add a DataTable to the collection with the Add method, passing no arguments, the table is given a default name, based on the order in which the table was added ("Table1", "Table2", and so on).

To get the index of a DataTable, use the IndexOf method.

NoteNote:

Returns false when two or more tables have the same name but different namespaces. The call does not succeed if there is any ambiguity when matching a table name to exactly one table.

The following example tests whether a table with the name "Suppliers" exists in the DataTableCollection.

private void TestForTableName()
{
    // Get the DataSet of a DataGrid.
    DataSet thisDataSet = (DataSet)DataGrid1.DataSource;

    // Get the DataTableCollection through the Tables property.
    DataTableCollection tablesCol = thisDataSet.Tables;

    // Check if the named table exists. 
    if (tablesCol.Contains("Suppliers")) 
        Console.WriteLine("Table named Suppliers exists");
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft