繁体中文
设为首页
加入收藏
当前位置:.Net技术首页 >> Asp.Net开发 >> 如何搜索DataTable中的值

如何搜索DataTable中的值

2007-07-15 08:00:00  作者:  来源:互联网  浏览次数:0  文字大小:【】【】【
简介: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++] pu...
关键字:DataTable 如何 搜索

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.

责任编辑:admin
相关文章