DataRowCollection.Find メソッド

定義

指定した PrimaryKey 値を使って DataRow を取得します。

オーバーロード

Find(Object[])

指定した主キー値が格納されている行を取得します。

Find(Object)

主キー値で指定した行を取得します。

注釈

パフォーマンスは O(log n) 操作である必要があります。

Find(Object[])

ソース:
DataRowCollection.cs
ソース:
DataRowCollection.cs
ソース:
DataRowCollection.cs

指定した主キー値が格納されている行を取得します。

public:
 System::Data::DataRow ^ Find(cli::array <System::Object ^> ^ keys);
public System.Data.DataRow? Find (object?[] keys);
public System.Data.DataRow Find (object[] keys);
member this.Find : obj[] -> System.Data.DataRow
Public Function Find (keys As Object()) As DataRow

パラメーター

keys
Object[]

検索する主キー値の配列。 配列の型は Object です。

戻り値

指定した主キー値が格納されている DataRow オブジェクト。指定した主キー値が DataRowCollection 内に存在しない場合は null 値。

例外

インデックス値に対応する行がありません。

このテーブルには主キーがありません。

次の例では、配列の値を使用して、 オブジェクトのコレクション内の特定の DataRow 行を検索します。 メソッドは、 が 3 つの主キー列と共に存在することを DataTable 前提としています。 値の配列を作成した後、コードでは、 メソッドと配列を使用 Find して、目的の特定のオブジェクトを取得します。

private void FindInMultiPKey(DataTable table)
{
    // 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.";

    DataRow foundRow = table.Rows.Find(findTheseVals);
    // Display column 1 of the found row.
    if(foundRow != null)
        Console.WriteLine(foundRow[1]);
}
 Private Sub FindInMultiPKey(ByVal table As DataTable)
    ' 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."

    Dim foundRow As DataRow = table.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

注釈

メソッドを Find 使用するには、オブジェクトが DataTable 属する DataRowCollection オブジェクトに、主キー列として指定された少なくとも 1 つの列が必要です。 2 つ以上の行の主キー値が同じである場合は、最初に見つかった行が返されます。 これは、 が false に設定されている場合 EnforceConstraints に発生します。 テーブルに複数のPrimaryKey主キーがある場合に列またはオブジェクトのDataColumn配列をPrimaryKey作成する方法の詳細については、 プロパティを参照してください。

こちらもご覧ください

適用対象

Find(Object)

ソース:
DataRowCollection.cs
ソース:
DataRowCollection.cs
ソース:
DataRowCollection.cs

主キー値で指定した行を取得します。

public:
 System::Data::DataRow ^ Find(System::Object ^ key);
public System.Data.DataRow? Find (object? key);
public System.Data.DataRow Find (object key);
member this.Find : obj -> System.Data.DataRow
Public Function Find (key As Object) As DataRow

パラメーター

key
Object

検索する DataRow の主キー値。

戻り値

指定した主キー値が格納されている DataRow。指定した主キー値が DataRowCollection 内に存在しない場合は null 値。

例外

このテーブルには主キーがありません。

次の例では、 メソッドを Find 使用して、 オブジェクトの DataRow コレクション内の主キー値 "2" を検索します。 メソッドは、必要に応じて値を変更できる特定 DataRow のオブジェクトを返します。

private void FindInPrimaryKeyColumn(DataTable table,
    long pkValue)
{
    // Find the number pkValue in the primary key
    // column of the table.
    DataRow foundRow = table.Rows.Find(pkValue);

    // Print the value of column 1 of the found row.
    if(foundRow != null)
        Console.WriteLine(foundRow[1]);
}
 Private Sub FindInPrimaryKeyColumn(ByVal table As DataTable, _
    ByVal pkValue As Long)
    ' Find the number pkValue in the primary key 
    ' column of the table.
    Dim foundRow As DataRow = table.Rows.Find(pkValue)

    ' Print the value of column 1 of the found row.
    If Not (foundRow Is Nothing) Then
        Console.WriteLine(foundRow(1).ToString())
    End If
End Sub

注釈

メソッドを Find 使用するには、オブジェクトが DataTable 属する DataRowCollection オブジェクトに、主キー列として指定された少なくとも 1 つの列が必要です。 主キー列を PrimaryKey 作成する方法の詳細については、 プロパティを参照してください。

こちらもご覧ください

適用対象