Export (0) Print
Expand All

Finding Rows

You can search for rows according to their sort key values by using the Find and FindRows methods of the DataView. The case sensitivity of search values in the Find and FindRows methods is determined by the CaseSensitive property of the underlying DataTable. Search values must match existing sort key values in their entirety in order to return a result.

The Find method returns an integer with the index of the DataRowView that matches the search criteria. If more than one row matches the search criteria, only the index of the first matching DataRowView is returned. If no matches are found, Find returns -1.

To return search results that match multiple rows, use the FindRows method. FindRows works just like the Find method, except that it returns a DataRowView array that references all matching rows in the DataView. If no matches are found, the DataRowView array will be empty.

To use the Find or FindRows methods you must specify a sort order either by setting ApplyDefaultSort to true or by using the Sort property. If no sort order is specified, an exception is thrown.

The Find and FindRows methods take an array of values as input whose length matches the number of columns in the sort order. In the case of a sort on a single column, you can pass a single value. For sort orders containing multiple columns, you pass an array of objects. Note that for a sort on multiple columns, the values in the object array must match the order of the columns specified in the Sort property of the DataView.

The following code example shows the Find method being called against a DataView with a single column sort order.

DataView custView = new DataView(custDS.Tables["Customers"], "", 
  "CompanyName", DataViewRowState.CurrentRows);

int rowIndex = custView.Find("The Cracker Box");

if (rowIndex == -1)
  Console.WriteLine("No match found.");
else
  Console.WriteLine("{0}, {1}",
    custView[rowIndex]["CustomerID"].ToString(),
    custView[rowIndex]["CompanyName"].ToString());

If your Sort property specifies multiple columns, you must pass an object array with the search values for each column in the order specified by the Sort property, as in the following code example.

DataView custView = new DataView(custDS.Tables["Customers"], "",
  "CompanyName, ContactName",
  DataViewRowState.CurrentRows);

DataRowView[] foundRows = 
  custView.FindRows(new object[] {"The Cracker Box", "Liu Wong"});

if (foundRows.Length == 0)
  Console.WriteLine("No match found.");
else
  foreach (DataRowView myDRV in foundRows)
    Console.WriteLine("{0}, {1}", myDRV["CompanyName"].ToString(), 
      myDRV["ContactName"].ToString());
Show:
© 2014 Microsoft