System.Data Namespace DataRowCollection Class
Returns the row containing the specified primary key values.
[ VB ]
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;
- keys
- An array of primary key values to find. The type of the array is Object.
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.
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.
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.
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 )
Response.Write ( foundRow [ 1 ] );
}
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 _
Response.Write ( foundRow ( 1 ).ToString ( ) )
End Sub |
|
C# |
VB |
DataRowCollection Members DataRowCollection.Find Overload List DataTable PrimaryKey Contains