Export (0) Print
Expand All

DataTable.Rows Property

Gets the collection of rows that belong to this table.

Namespace:  System.Data
Assembly:  System.Data (in System.Data.dll)

[BrowsableAttribute(false)]
public DataRowCollection Rows { get; }

Property Value

Type: System.Data.DataRowCollection
A DataRowCollection that contains DataRow objects; otherwise a null value if no DataRow objects exist.

To create a new DataRow, you must use the NewRow method to return a new object. Such an object is automatically configured according to the schema defined for the DataTable through its collection of DataColumn objects. 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.

Each DataRow in the collection represents a row of data in the table. To commit a change to the value of a column in the row, you must invoke the AcceptChanges method.

The following shows two examples of returning and setting rows. The first example uses the Rows property and prints the value of each column for every row. The second example uses the DataTable object's NewRow method to create a new DataRow object with the schema of the DataTable. 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)
            {
                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);
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft