次の方法で共有


DataTable.Rows プロパティ

このテーブルに属する行のコレクションを取得します。

Public ReadOnly Property Rows As DataRowCollection
[C#]
public DataRowCollection Rows {get;}
[C++]
public: __property DataRowCollection* get_Rows();
[JScript]
public function get Rows() : DataRowCollection;

プロパティ値

DataRow オブジェクトが格納されている DataRowCollectionDataRow オブジェクトが存在しない場合は null 値。

解説

新しい DataRow を作成するには、 NewRow メソッドを使用して、新しいオブジェクトを返す必要があります。このようなオブジェクトは、 DataColumn オブジェクトのコレクションを通じて DataTable に対して定義されたスキーマに従って、自動的に設定されます。新しい行を作成し、行内の各列の値を設定した後、Add メソッドを使用してこの行を DataRowCollection に追加します。

コレクション内の各 DataRow は、テーブル内のデータ行を表します。行内の列の値に対して行った変更をコミットするには、 AcceptChanges メソッドを呼び出す必要があります。

使用例

[Visual Basic, C#, C++] 行を返し、設定する 2 つの例を次に示します。最初の例では、 Rows プロパティを使用し、各行の各列の値を出力します。2 番目の例では、 DataTable オブジェクトの NewRow メソッドを使用して、 DataTable のスキーマで新しい DataRow オブジェクトを作成します。行の値を設定した後、Add メソッドを使用してこの行を DataRowCollection に追加します。

 
Private Sub PrintRows(myDataSet As DataSet)
    ' For each table in the DataSet, print the values of each row.
    Dim thisTable As DataTable
    For Each thisTable In  myDataSet.Tables
        ' For each row, print the values of each column.
        Dim myRow As DataRow
        For Each myRow In  thisTable.Rows
            Dim myCol As DataColumn
            For Each myCol In  thisTable.Columns
                Console.WriteLine(myRow(myCol))
            Next myCol
        Next myRow
    Next thisTable
End Sub
   
   
Private Sub AddARow(ds As DataSet)
    Dim t As DataTable
    t = ds.Tables("Suppliers")
    ' Use the NewRow method to create a DataRow with the table's schema.
    Dim newRow As DataRow = t.NewRow()
    ' Set values in the columns:
    newRow("CompanyID") = "NewCompanyID"
    newRow("CompanyName") = "NewCompanyName"
    ' Add the row to the rows collection.
    t.Rows.Add(newRow)
End Sub

[C#] 
private void PrintRows(DataSet myDataSet){
   // For each table in the DataSet, print the values of each row.
 
   foreach(DataTable thisTable in myDataSet.Tables){
      // For each row, print the values of each column.
      foreach(DataRow myRow in thisTable.Rows){
         foreach(DataColumn myCol in thisTable.Columns){
            Console.WriteLine(myRow[myCol]);
         }
       }
   }
}


private void AddARow(DataSet ds){
   DataTable t;
   t = ds.Tables["Suppliers"];
   // Use the NewRow method to create a DataRow with the table's schema.
   DataRow newRow = t.NewRow();
   // Set values in the columns:
   newRow["CompanyID"] = "NewCompanyID";
   newRow["CompanyName"] = "NewCompanyName";
   // Add the row to the rows collection.
   t.Rows.Add(newRow);
}

[C++] 
private:
 void PrintRows(DataSet* myDataSet){
    // For each table in the DataSet, print the values of each row.
  
    System::Collections::IEnumerator* myEnum = myDataSet->Tables->GetEnumerator();
    while (myEnum->MoveNext())
    {
       DataTable* thisTable = __try_cast<DataTable*>(myEnum->Current);
       // For each row, print the values of each column.
       System::Collections::IEnumerator* myEnum1 = thisTable->Rows->GetEnumerator();
       while (myEnum1->MoveNext())
       {
          DataRow* myRow = __try_cast<DataRow*>(myEnum1->Current);
          System::Collections::IEnumerator* myEnum2 = thisTable->Columns->GetEnumerator();
          while (myEnum2->MoveNext())
          {
             DataColumn* myCol = __try_cast<DataColumn*>(myEnum2->Current);
             Console::WriteLine(myRow->Item[myCol]);
          }
        }
    }
 }
  
 void AddARow(DataSet* ds){
    DataTable* t;
    t = ds->Tables->Item[S"Suppliers"];
    // Use the NewRow method to create a DataRow with the table's schema.
    DataRow* newRow = t->NewRow();
    // Set values in the columns:
    newRow->Item[S"CompanyID"] = S"NewCompanyID";
    newRow->Item[S"CompanyName"] = S"NewCompanyName";
    // Add the row to the rows collection.
    t->Rows->Add(newRow);
 }

[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

参照

DataTable クラス | DataTable メンバ | System.Data 名前空間 | AcceptChanges | DataRow | DataRowCollection | NewRow