DataRowCollection.Contains Method

Definition

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

Overloads

Contains(Object)

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

Contains(Object[])

Gets a value that indicates whether the primary key columns of any row in the collection contain the values specified in the object array.

Contains(Object)

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

public:
 bool Contains(System::Object ^ key);
public bool Contains (object key);
member this.Contains : obj -> bool
Public Function Contains (key As Object) As Boolean

Parameters

key
Object

The value of the primary key to test for.

Returns

Boolean

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

Exceptions

The table does not have a primary key.

Examples

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

Remarks

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.

See also

Contains(Object[])

Gets a value that indicates whether the primary key columns of any row in the collection contain the values specified in the object array.

public:
 bool Contains(cli::array <System::Object ^> ^ keys);
public bool Contains (object[] keys);
member this.Contains : obj[] -> bool
Public Function Contains (keys As Object()) As Boolean

Parameters

keys
Object[]

An array of primary key values to test for.

Returns

Boolean

true if the DataRowCollection contains a DataRow with the specified key values; otherwise, false.

Exceptions

The table does not have a primary key.

Examples

The following Visual Basic example uses the Contains method to find a particular row in a DataRowCollection object. The example creates an array of values, one element for each primary key in the table, and then passes the array to the method to return a true or false.

Private Sub ContainsArray()
   ' This example assumes that the DataTable object contains two
   ' DataColumn objects designated as primary keys.
   ' The table has two primary key columns.
   Dim arrKeyVals(1) As Object
   Dim table As DataTable = CType(DataGrid1.DataSource, DataTable)
   Dim rowCollection As DataRowCollection = table.Rows
   arrKeyVals(0) = "Hello"
   arrKeyVals(1) = "World"
   label1.Text = rowCollection.Contains(arrKeyVals).ToString()
End Sub

Remarks

To use the Contains method with an array of values, the DataTable object to which the DataRowCollection object belongs must have an array of columns designated as primary keys. See the PrimaryKey property for more information about how to create an array of primary key columns. The number of array elements must correspond to the number of primary key columns in the DataTable.

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

See also

Applies to