如何搜索DataTable中的值

80酷酷网    80kuku.com

  Gets a specified DataRow.

Overload List
Gets the row specified by the primary key value.

[Visual Basic] Overloads Public Function Find(Object) As DataRow
[C#] public DataRow Find(object);
[C++] public: DataRow* Find(Object*);
[JScript] public function Find(Object) : DataRow;
Gets the row containing the specified primary key values.

[Visual Basic] Overloads Public Function Find(Object()) As DataRow
[C#] public DataRow Find(object[]);
[C++] public: DataRow* Find(Object*[]);
[JScript] public function Find(Object[]) : DataRow;
Example
[Visual Basic, C#] 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.

[Visual Basic, C#] Note   This example shows how to use one of the overloaded versions of Find. For other examples that might be available, see the individual overload topics.
[Visual Basic]
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
     Console.WriteLine(foundRow(1).ToString())
   End If
End Sub
[C#]
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)
      Console.WriteLine(foundRow[1]);
}
[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button  in the upper-left corner of the page.

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: