System.Data Namespace DataRowCollection Class
Returns a specified DataRow.
1. Returns the row specified by the primary key value.
2. Returns the row containing the specified primary key values.
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.
NOTE: This example uses one of the overloaded versions of Find. For other examples that may be available, see the individual overload topics.
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