DataTable.Rows 属性

定义

获取属于该表的行的集合。Gets the collection of rows that belong to this table.

public:
 property System::Data::DataRowCollection ^ Rows { System::Data::DataRowCollection ^ get(); };
[System.ComponentModel.Browsable(false)]
public System.Data.DataRowCollection Rows { get; }
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
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

一个包含 DataRowCollection 对象的 DataRowA DataRowCollection that contains DataRow objects.

属性

示例

下面演示了返回和设置行的两个示例。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 对象,该对象具有的架构 DataTableThe second example uses the DataTable object's NewRow method to create a new DataRow object with the schema of the DataTable. 设置行值后,通过方法将行添加到 DataRowCollection AddAfter 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)
            {
                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 方法返回新的对象。To create a new DataRow, you must use the NewRow method to return a new object. 此类对象根据为其对象集合定义的架构自动进行配置 DataTable DataColumnSuch an object is automatically configured according to the schema defined for the DataTable through its collection of DataColumn objects. 创建新行并为行中的每个列设置值后,使用方法将行添加到中 DataRowCollection AddAfter 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.

适用于

另请参阅