Find
Int32rowIndex
= DataView.Find(ObjectsortKey
); Int32rowIndex
= DataView.Find(Object[]sortKey
);
Returns the index of a single matching row, using the current
DataView
sort order. For example, if you have a
sort defined on the ContactName
column of the
Customers
table, you can use the Find( )
method to search for a row with a specific
ContactName
. If no match is found, the
Find( )
method returns -1. If there are multiple
matches, only the first is returned.
The Find( )
method requires exact matches. You
can’t perform a partial match (for example, by
supplying just a first name for the ContactName) or use a wildcard.
Object
sortKey
Object[]
sortKey
The sortKey
specifies the value you are
searching for. The data type you use should match the data type of
the column used for the search criteria. If you create a sort
expression that incorporates information from multiple columns, you
must use the overloaded version of the Find( )
method that accepts an array with search values for all columns, in
the same order.
The following code snippet searches for an exact match of the
ContactName
field:
DataView view = new DataView(ds.Tables["Customers"]); view.Sort = "ContactName"; int rowIndex = view.Find("Roland Mendel"); if (rowIndex == -1) { Console.WriteLine("No match found."); } else { Console.WriteLine(view[rowIndex]["CustomerID"].ToString() + " is a match."); }
To use the Find( )
method, you must have defined a
sort order for a DataView
, either by setting the
RowFilter
property or the
ApplyDefaultSort
property. If
it’s possible that your search will match more than
one row, use the FindRows( )
method instead of the
Find( )
method.
The case-sensitivity of search values for the Find( )
method is determined by the
CaseSensitive
property of the underlying
DataTable
.