英語で読む

次の方法で共有


DataTable.Rows プロパティ

定義

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

[System.ComponentModel.Browsable(false)]
public System.Data.DataRowCollection Rows { get; }
[System.ComponentModel.Browsable(false)]
[System.Data.DataSysDescription("DataTableRowsDescr")]
public System.Data.DataRowCollection Rows { get; }

プロパティ値

DataRowCollection オブジェクトを含む DataRow

属性

行の取得と設定の 2 つの例を次に示します。 最初の例では、 プロパティを Rows 使用し、各行の各列の値を出力します。 2 番目の例では、 オブジェクトの NewRow メソッドをDataTable使用して、 のスキーマを使用して新しいDataRowオブジェクトをDataTable作成します。 行の値を設定すると、 メソッドを使用してAdd行が にDataRowCollection追加されます。

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

private void AddARow(DataSet dataSet)
{
    DataTable table;
    table = dataSet.Tables["Suppliers"];
    // Use the NewRow method to create a DataRow with
    // the table's schema.
    DataRow newRow = table.NewRow();

    // Set values in the columns:
    newRow["CompanyID"] = "NewCompanyID";
    newRow["CompanyName"] = "NewCompanyName";

    // Add the row to the rows collection.
    table.Rows.Add(newRow);
}

注釈

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

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

適用対象

こちらもご覧ください