Contains Method (Object[])
Collapse the table of content
Expand the table of content

DataRowCollection.Contains Method (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.

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

public bool Contains (
	Object[] keys
public boolean Contains (
	Object[] keys
public function Contains (
	keys : Object[]
) : boolean
Not applicable.



An array of primary key values to test for.

Return Value

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

Exception typeCondition


The table does not have a primary key.

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.

The following 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

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

Community Additions

© 2016 Microsoft