This documentation is archived and is not being maintained.

DataRowCollection.Contains Method (Object)

Gets a value that indicates whether the primary key of any row in the collection contains the specified value.

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

public bool Contains (
	Object key
)
public boolean Contains (
	Object key
)
public function Contains (
	key : Object
) : boolean
Not applicable.

Parameters

key

The value of the primary key to test for.

Return Value

true if the collection contains a DataRow with the specified primary key value; otherwise, false.

Exception typeCondition

MissingPrimaryKeyException

The table does not have a primary key.

To use the Contains method, the DataTable object to which the DataRowCollection object belongs to must have at least one column designated as a primary key column. See the PrimaryKey property for more information about how to create a primary key column.

As soon as you have determined that a row contains the specified value, you can use the Find method to return the specific DataRow object that has the value.

The following example uses the Contains method to determine whether a DataRowCollection object contains a specific value.

 Private Sub ColContains()
    Dim table As DataTable = CType(DataGrid1.DataSource, DataTable)
    Dim rowCollection As DataRowCollection = table.Rows
    If rowCollection.Contains(Edit1.Text) Then
       Label1.Text = "At least one row contains " & Edit1.Text 
    Else
       Label1.Text = "No row contains the value in its primary key field"
    End If
End Sub

Windows 98, Windows Server 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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0
Show: