尋找資料列Finding Rows

您可以使用 FindFindRowsDataView 方法,依照資料列的排序索引鍵值來搜尋資料列。You can search for rows according to their sort key values by using the Find and FindRows methods of the DataView. FindFindRows方法中搜尋值的區分大小寫,是由基礎DataTableCaseSensitive屬性所決定。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.

Find方法會傳回一個整數,其中包含符合搜尋DataRowView條件之的索引。The Find method returns an integer with the index of the DataRowView that matches the search criteria. 如果有多個資料列符合搜尋準則,則只會傳回第一個相符DataRowView的索引。If more than one row matches the search criteria, only the index of the first matching DataRowView is returned. 如果找不到相符專案,則Find會傳回-1。If no matches are found, Find returns -1.

若要傳回符合多個資料列的搜尋結果,請使用FindRows方法。To return search results that match multiple rows, use the FindRows method. FindRows的運作方式就像Find方法,不同之處在于它會傳回參考DataView中所有相符資料列的DataRowView陣列。FindRows works just like the Find method, except that it returns a DataRowView array that references all matching rows in the DataView. 如果找不到相符專案, DataRowView陣列將會是空的。If no matches are found, the DataRowView array will be empty.

若要使用FindFindRows方法,您必須指定排序次序,方法是將ApplyDefaultSort設定為true ,或使用sort屬性。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.

FindFindRows方法會將值的陣列當做輸入,其長度符合排序次序中的資料行數目。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. 請注意,針對多個資料行進行排序時,物件陣列中的值必須符合DataViewsort屬性中所指定的資料行順序。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.

下列程式碼範例示範如何針對具有單一資料行排序次序的DataView呼叫Find方法。The following code example shows the Find method being called against a DataView with a single column sort order.

Dim custView As DataView = _  
  New DataView(custDS.Tables("Customers"), "", _  
  "CompanyName", DataViewRowState.CurrentRows)  
  
Dim rowIndex As Integer = custView.Find("The Cracker Box")  
  
If rowIndex = -1 Then  
  Console.WriteLine("No match found.")  
Else  
  Console.WriteLine("{0}, {1}", _  
    custView(rowIndex)("CustomerID").ToString(), _  
    custView(rowIndex)("CompanyName").ToString())  
End If  
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());  

如果您的Sort屬性指定多個資料行,您必須以Sort屬性所指定的順序,以每個資料行的搜尋值傳遞物件陣列,如下列程式碼範例所示。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.

Dim custView As DataView = _  
  New DataView(custDS.Tables("Customers"), "", _  
  "CompanyName, ContactName", _  
  DataViewRowState.CurrentRows)  
  
Dim foundRows() As DataRowView = _  
  custView.FindRows(New object() {"The Cracker Box", "Liu Wong"})  
  
If foundRows.Length = 0 Then  
  Console.WriteLine("No match found.")  
Else  
  Dim myDRV As DataRowView  
  For Each myDRV In foundRows  
    Console.WriteLine("{0}, {1}", _  
      myDRV("CompanyName").ToString(), myDRV("ContactName").ToString())  
  Next  
End If  
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());  

另請參閱See also