Export (0) Print
Expand All

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)

member Contains : 
        key:Object -> bool

Parameters

key
Type: System.Object

The value of the primary key to test for.

Return Value

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

ExceptionCondition
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 Visual Basic 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

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft