DataTable.Rows 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得屬於此表的列集合。
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
屬性值
包含DataRow物件的 ADataRowCollection。
- 屬性
範例
以下展示兩個返回與設定列的範例。 第一個範例使用了屬性 Rows ,並對每一列列印每欄的值。 第二個範例使用 DataTable 物件的方法 NewRow ,建立 DataRow 一個帶有結構 DataTable的新物件。 設定好列值後,該列會透過方法加入 。DataRowCollectionAdd
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 新物件的方法。 此類物件會根據其物件集合DataColumn所定義DataTable的結構自動配置。 建立新列並設定該列中每欄的值後,使用該Add方法將該列加入 。DataRowCollection
集合中的每個 DataRow 資料代表表格中的一列資料。 要提交該列欄位的變更,必須呼叫該 AcceptChanges 方法。