Export (0) Print
Expand All

DataRowCollection.Find Method (Object[])

.NET Framework 1.1

Gets the row containing the specified primary key values.

[Visual Basic]
Overloads Public Function Find( _
   ByVal keys() As Object _
) As DataRow
[C#]
public DataRow Find(
 object[] keys
);
[C++]
public: DataRow* Find(
 Object* keys __gc[]
);
[JScript]
public function Find(
   keys : Object[]
) : DataRow;

Parameters

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

Return Value

An array of DataRow objects containing the primary key values specified; otherwise a null value if the primary key values do not exist in the DataRowCollection.

Exceptions

Exception Type Condition
IndexOutOfRangeException No row corresponds to that index value.
MissingPrimaryKeyException The table does not have a primary key.

Remarks

To use the Find 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 details on creating a PrimaryKey column, or an array of DataColumn objects when the table has more than one primary key.

Example

[Visual Basic, C#, C++] The following example uses the values of an array to find a specific row in a collection of DataRow objects. The method presumes 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 desired.

[Visual Basic] 
Private Sub FindInMultiPKey(ByVal myTable As DataTable)
   Dim foundRow As DataRow
   ' Create an array for the key values to find.
   Dim findTheseVals(2) As Object
   ' Set the values of the keys to find.
   findTheseVals(0) = "John"
   findTheseVals(1) = "Smith"
   findTheseVals(2) = "5 Main St."
   foundRow = myTable.Rows.Find(findTheseVals)
   ' Display column 1 of the found row.
   If Not (foundRow Is Nothing) Then
     Console.WriteLine(foundRow(1).ToString())
   End If
End Sub

[C#] 
private void FindInMultiPKey(DataTable myTable){
    DataRow foundRow;
    // 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.";
    foundRow = myTable.Rows.Find(findTheseVals);
    // Display column 1 of the found row.
    if(foundRow != null)
      Console.WriteLine(foundRow[1]);
 }

[C++] 
private:
void FindInMultiPKey(DataTable* myTable){
    DataRow* foundRow;
    // Create an array for the key values to find.
    Object* findTheseVals[] = new Object*[3];
    // Set the values of the keys to find.
    findTheseVals[0] = S"John";
    findTheseVals[1] = S"Smith";
    findTheseVals[2] = S"5 Main St.";
    foundRow = myTable->Rows->Find(findTheseVals);
    // Display column 1 of the found row.
    if(foundRow != 0)
      Console::WriteLine(foundRow->Item[1]);
 }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

DataRowCollection Class | DataRowCollection Members | System.Data Namespace | DataRowCollection.Find Overload List | DataTable | PrimaryKey | Contains

Show:
© 2014 Microsoft