DataRowCollection.Find Method

Definition

Gets a DataRow using the specified PrimaryKey value.

Overloads

Find(Object[])

Gets the row that contains the specified primary key values.

Find(Object)

Gets the row specified by the primary key value.

Remarks

Performance should be an O(log n) operation.

Find(Object[])

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

Gets the row that contains the specified primary key values.

C#
public System.Data.DataRow? Find(object?[] keys);
C#
public System.Data.DataRow Find(object[] keys);

Parameters

keys
Object[]

An array of primary key values to find. The type of the array is Object.

Returns

A DataRow object that contains the primary key values specified; otherwise a null value if the primary key value does not exist in the DataRowCollection.

Exceptions

No row corresponds to that index value.

The table does not have a primary key.

Examples

The following example uses the values of an array to find a specific row in a collection of DataRow objects. The method assumes that a DataTable exists with three primary key columns. After creating an array of the values, the code uses the Find method with the array to get the particular object that you want.

C#
private void FindInMultiPKey(DataTable table)
{
    // Create an array for the key values to find.
    object[]findTheseVals = new object[3];

    // Set the values of the keys to find.
    findTheseVals[0] = "John";
    findTheseVals[1] = "Smith";
    findTheseVals[2] = "5 Main St.";

    DataRow foundRow = table.Rows.Find(findTheseVals);
    // Display column 1 of the found row.
    if(foundRow != null)
        Console.WriteLine(foundRow[1]);
}

Remarks

To use the Find method, the DataTable object to which the DataRowCollection object belongs must have at least one column designated as a primary key column. When two or more rows have the same primary key value, then the first row found is returned. This occurs when EnforceConstraints is set to false. See the PrimaryKey property for more information about how to create a PrimaryKey column, or an array of DataColumn objects when the table has more than one primary key.

See also

Applies to

.NET 10 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, 10
.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

Find(Object)

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

Gets the row specified by the primary key value.

C#
public System.Data.DataRow? Find(object? key);
C#
public System.Data.DataRow Find(object key);

Parameters

key
Object

The primary key value of the DataRow to find.

Returns

A DataRow that contains the primary key value specified; otherwise a null value if the primary key value does not exist in the DataRowCollection.

Exceptions

The table does not have a primary key.

Examples

The following example uses the Find method to find the primary key value "2" in a collection of DataRow objects. The method returns the specific DataRow object letting you change its values, as needed.

C#
private void FindInPrimaryKeyColumn(DataTable table,
    long pkValue)
{
    // Find the number pkValue in the primary key
    // column of the table.
    DataRow foundRow = table.Rows.Find(pkValue);

    // Print the value of column 1 of the found row.
    if(foundRow != null)
        Console.WriteLine(foundRow[1]);
}

Remarks

To use the Find method, the DataTable object to which the DataRowCollection object belongs 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.

See also

Applies to

.NET 10 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, 10
.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