This documentation is archived and is not being maintained.

DataTable.Rows Property

Gets the collection of rows that belong to this table.

[Visual Basic]
Public ReadOnly Property Rows As DataRowCollection
[C#]
public DataRowCollection Rows {get;}
[C++]
public: __property DataRowCollection* get_Rows();
[JScript]
public function get Rows() : DataRowCollection;

Property Value

A DataRowCollection that contains DataRow objects; otherwise a null value if no DataRow objects exist.

Remarks

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.

Example

[Visual Basic, C#, C++] 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.

[Visual Basic] 
Private Sub PrintRows(myDataSet As DataSet)
    ' For each table in the DataSet, print the values of each row.
    Dim thisTable As DataTable
    For Each thisTable In  myDataSet.Tables
        ' For each row, print the values of each column.
        Dim myRow As DataRow
        For Each myRow In  thisTable.Rows
            Dim myCol As DataColumn
            For Each myCol In  thisTable.Columns
                Console.WriteLine(myRow(myCol))
            Next myCol
        Next myRow
    Next thisTable
End Sub
   
   
Private Sub AddARow(ds As DataSet)
    Dim t As DataTable
    t = ds.Tables("Suppliers")
    ' Use the NewRow method to create a DataRow with the table's schema.
    Dim newRow As DataRow = t.NewRow()
    ' Set values in the columns:
    newRow("CompanyID") = "NewCompanyID"
    newRow("CompanyName") = "NewCompanyName"
    ' Add the row to the rows collection.
    t.Rows.Add(newRow)
End Sub

[C#] 
private void PrintRows(DataSet myDataSet){
   // For each table in the DataSet, print the values of each row.
 
   foreach(DataTable thisTable in myDataSet.Tables){
      // For each row, print the values of each column.
      foreach(DataRow myRow in thisTable.Rows){
         foreach(DataColumn myCol in thisTable.Columns){
            Console.WriteLine(myRow[myCol]);
         }
       }
   }
}


private void AddARow(DataSet ds){
   DataTable t;
   t = ds.Tables["Suppliers"];
   // Use the NewRow method to create a DataRow with the table's schema.
   DataRow newRow = t.NewRow();
   // Set values in the columns:
   newRow["CompanyID"] = "NewCompanyID";
   newRow["CompanyName"] = "NewCompanyName";
   // Add the row to the rows collection.
   t.Rows.Add(newRow);
}

[C++] 
private:
 void PrintRows(DataSet* myDataSet){
    // For each table in the DataSet, print the values of each row.
  
    System::Collections::IEnumerator* myEnum = myDataSet->Tables->GetEnumerator();
    while (myEnum->MoveNext())
    {
       DataTable* thisTable = __try_cast<DataTable*>(myEnum->Current);
       // For each row, print the values of each column.
       System::Collections::IEnumerator* myEnum1 = thisTable->Rows->GetEnumerator();
       while (myEnum1->MoveNext())
       {
          DataRow* myRow = __try_cast<DataRow*>(myEnum1->Current);
          System::Collections::IEnumerator* myEnum2 = thisTable->Columns->GetEnumerator();
          while (myEnum2->MoveNext())
          {
             DataColumn* myCol = __try_cast<DataColumn*>(myEnum2->Current);
             Console::WriteLine(myRow->Item[myCol]);
          }
        }
    }
 }
  
 void AddARow(DataSet* ds){
    DataTable* t;
    t = ds->Tables->Item[S"Suppliers"];
    // Use the NewRow method to create a DataRow with the table's schema.
    DataRow* newRow = t->NewRow();
    // Set values in the columns:
    newRow->Item[S"CompanyID"] = S"NewCompanyID";
    newRow->Item[S"CompanyName"] = S"NewCompanyName";
    // Add the row to the rows collection.
    t->Rows->Add(newRow);
 }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

DataTable Class | DataTable Members | System.Data Namespace | AcceptChanges | DataRow | DataRowCollection | NewRow

Show: