DataTable.Rows 屬性

定義

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

public:
 property System::Data::DataRowCollection ^ Rows { System::Data::DataRowCollection ^ get(); };
[System.ComponentModel.Browsable(false)]
public System.Data.DataRowCollection Rows { get; }
[System.ComponentModel.Browsable(false)]
[System.Data.DataSysDescription("DataTableRowsDescr")]
public System.Data.DataRowCollection Rows { get; }
[<System.ComponentModel.Browsable(false)>]
member this.Rows : System.Data.DataRowCollection
[<System.ComponentModel.Browsable(false)>]
[<System.Data.DataSysDescription("DataTableRowsDescr")>]
member this.Rows : System.Data.DataRowCollection
Public ReadOnly Property Rows As DataRowCollection

屬性值

包含 DataRowCollection 物件的 DataRow

屬性

範例

以下顯示傳回和設定數據列的兩個範例。 第一個範例會 Rows 使用 屬性,並列印每個數據列的每個數據行值。 第二個範例會DataTable使用 物件的 NewRow 方法來建立具有 架構DataTable的新DataRow物件。 設定資料欄值之後,資料列會透過 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);
}
Private Sub PrintRows(dataSet As DataSet)
     ' For each table in the DataSet, print the values of each row.
     Dim thisTable As DataTable
     For Each thisTable In  dataSet.Tables
         ' For each row, print the values of each column.
         Dim row As DataRow
         For Each row In  thisTable.Rows
             Dim column As DataColumn
             For Each column In  thisTable.Columns
                 Console.WriteLine(row(column))
             Next column
         Next row
     Next thisTable
End Sub
    
    
Private Sub AddARow(dataSet As DataSet)
    Dim table As DataTable = dataSet.Tables("Suppliers")
    ' Use the NewRow method to create a DataRow 
    'with the table's schema.
    Dim newRow As DataRow = table.NewRow()

    ' Set values in the columns:
    newRow("CompanyID") = "NewCompanyID"
    newRow("CompanyName") = "NewCompanyName"

    ' Add the row to the rows collection.
    table.Rows.Add(newRow)
End Sub

備註

若要建立新的 DataRow,您必須使用 NewRow 方法來傳回新的 物件。 這類物件會根據透過物件集合DataColumnDataTable定義的架構自動設定。 建立新的資料列並設定資料列中每個資料列的值之後,請使用 Add 方法將資料列新增至 DataRowCollection

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

適用於

另請參閱