DataTable.Rows DataTable.Rows DataTable.Rows DataTable.Rows Property


取得屬於這個資料表的資料列集合。Gets the collection of rows that belong to this table.

 property System::Data::DataRowCollection ^ Rows { System::Data::DataRowCollection ^ get(); };
public System.Data.DataRowCollection Rows { get; }
member this.Rows : System.Data.DataRowCollection
Public ReadOnly Property Rows As DataRowCollection


DataRowCollection (包含 DataRow 物件),否則為 Null 值 (如果 DataRow 物件不存在)。A DataRowCollection that contains DataRow objects; otherwise a null value if no DataRow objects exist.


以下顯示傳回和設定資料列的兩個範例。The following shows two examples of returning and setting rows. 第一個範例會使用Rows屬性,並列印每個資料列的每個資料行的值。The first example uses the Rows property and prints the value of each column for every row. 第二個範例會DataTable使用物件NewRow的方法,利用的DataRow架構DataTable來建立新的物件。The second example uses the DataTable object's NewRow method to create a new DataRow object with the schema of the DataTable. 設定列值之後,資料列會DataRowCollection Add透過方法加入至。After setting the row values, the row is added to the DataRowCollection through the Add method.

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)

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.
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
             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.
End Sub


若要建立新DataRow的,您必須NewRow使用方法來傳回新的物件。To create a new DataRow, you must use the NewRow method to return a new object. 這類物件會根據透過其DataTable DataColumn物件集合所定義的架構自動設定。Such an object is automatically configured according to the schema defined for the DataTable through its collection of DataColumn objects. 在建立新的資料列,並在資料列中設定每個資料行的值之後, DataRowCollectionAdd使用方法,將資料列加入至。After creating a new row and setting the values for each column in the row, add the row to the DataRowCollection using the Add method.

集合DataRow中的每個都代表資料表中的資料列。Each DataRow in the collection represents a row of data in the table. 若要認可資料列中資料行值的變更,您必須AcceptChanges叫用方法。To commit a change to the value of a column in the row, you must invoke the AcceptChanges method.