Edit

Share via


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)

Source:
DataRowCollection.cs
Source:
DataRowCollection.cs
Source:
DataRowCollection.cs

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

public bool Contains (object? key);
public bool Contains (object key);

Parameters

key
Object

The value of the primary key to test for.

Returns

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

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Contains(Object[])

Source:
DataRowCollection.cs
Source:
DataRowCollection.cs
Source:
DataRowCollection.cs

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 (object?[] keys);
public bool Contains (object[] keys);

Parameters

keys
Object[]

An array of primary key values to test for.

Returns

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

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1