DataTable.Rows 屬性

定義

取得屬於這個資料表的資料列集合。

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

屬性值

包含 DataRowCollection 物件的 DataRow

屬性

範例

以下顯示傳回和設定數據列的兩個範例。 第一個範例會 Rows 使用 屬性,並列印每個數據列的每個數據行值。 第二個範例會DataTable使用 物件的 NewRow 方法來建立具有 架構DataTable的新DataRow物件。 設定資料欄值之後,資料列會透過 Add 方法新增至 DataRowCollection

C#
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 方法來傳回新的 物件。 這類物件會根據透過物件集合DataColumnDataTable定義的架構自動設定。 建立新的資料列並設定資料列中每個資料列的值之後,請使用 Add 方法將資料列新增至 DataRowCollection

DataRow集合中的每個代表數據表中的數據列。 若要認可數據列中數據行值的變更,您必須叫用 AcceptChanges 方法。

適用於

產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

另請參閱