次の方法で共有


DataRowCollection.Find メソッド (Object)

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

Overloads Public Function Find( _
   ByVal key As Object _) As DataRow
[C#]
public DataRow Find(objectkey);
[C++]
public: DataRow* Find(Object* key);
[JScript]
public function Find(
   key : Object) : DataRow;

パラメータ

  • key
    検索する DataRow の主キー値。

戻り値

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

例外

例外の種類 条件
MissingPrimaryKeyException このテーブルには主キーがありません。

解説

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

使用例

[Visual Basic, C#, C++] Find メソッドを使用して、 DataRow オブジェクトのコレクション内にある主キー値 "2" を検索する例を次に示します。このメソッドによって特定の DataRow オブジェクトが返されるため、必要に応じてオブジェクトの値を変更できます。

 
Private Sub FindInPrimaryKeyColumn(ByVal myTable As DataTable, ByVal n As Long)
   ' Find the number n in the primary key column of the table.
   Dim foundRow As DataRow = myTable.Rows.Find(n)
   ' 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

[C#] 
private void FindInPrimaryKeyColumn(DataTable myTable, long n){
   // Find the number n in the primary key column of the table.
   DataRow foundRow = myTable.Rows.Find(n);
   // Print the value of column 1 of the found row.
   if(foundRow != null)
     Console.WriteLine(foundRow[1]);
}

[C++] 
private:
 void FindInPrimaryKeyColumn(DataTable* myTable, long n){
    // Find the number n in the primary key column of the table.
    DataRow* foundRow = myTable->Rows->Find(__box(n));
    // Print the value of column 1 of the found row.
    if(foundRow != 0)
      Console::WriteLine(foundRow->Item[1]);
 }

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

DataRowCollection クラス | DataRowCollection メンバ | System.Data 名前空間 | DataRowCollection.Find オーバーロードの一覧 | DataTable | PrimaryKey | Contains