Export (0) Print
Expand All
1 out of 2 rated this helpful - Rate this topic

DataRowCollection Class

Represents a collection of rows for a DataTable.

For a list of all members of this type, see DataRowCollection Members.

System.Object
   System.Data.InternalDataCollectionBase
      System.Data.DataRowCollection

[Visual Basic]
<Serializable>
Public Class DataRowCollection
   Inherits InternalDataCollectionBase
[C#]
[Serializable]
public class DataRowCollection : InternalDataCollectionBase
[C++]
[Serializable]
public __gc class DataRowCollection : public
   InternalDataCollectionBase
[JScript]
public
   Serializable
class DataRowCollection extends InternalDataCollectionBase

Thread Safety

This type is safe for multithreaded read operations. You must synchronize any write operations.

Remarks

The DataRowCollection is a major component of the DataTable. While the DataColumnCollection defines the schema of the table, the DataRowCollection contains the actual data for the table, where each DataRow in the DataRowCollection represents a single row.

You can call the Add and Remove methods to insert and delete DataRow objects from the DataRowCollection. You can also call the Find method to search for DataRow objects that contain specific values in primary key columns, and the Contains method to search character-based data for single words or phrases.

Example

[Visual Basic, C#, C++] The first example below prints the value of column 1 for every row in a DataRowCollection. The second example adds a new row created with the NewRow method to the DataRowCollection.

[Visual Basic] 
Private Sub ShowRows(Byval myTable As DataTable)
    ' Print the number of rows in the collection.
    Console.WriteLine(myTable.Rows.Count)
    Dim row  As DataRow
    ' Print the value of columns 1 in each row
    For Each row In myTable.Rows
        Console.WriteLine(row(1))
    Next
 End Sub
 
 Private Sub AddRow(ByVal myTable As DataTable)
    Dim newRow As DataRow
    ' Instantiate a new row using the NewRow method.
    newRow = myTable.NewRow()
    ' Insert code to fill the row with values.
    ' Add the row to the DataRowCollection.
    myTable.Rows.Add(newRow)
 End Sub

[C#] 
private void ShowRows(DataTable myTable){
    // Print the number of rows in the collection.
    Console.WriteLine(myTable.Rows.Count);
    // Print the value of columns 1 in each row
    foreach(DataRow row in myTable.Rows){
        Console.WriteLine(row[1]);
    }
 }
 
 private void AddRow(DataTable myTable){
    DataRow newRow;
    DataRowCollection rc = myTable.Rows;
    // Instantiate a new row using the NewRow method.
    newRow = myTable.NewRow();
    // Insert code to fill the row with values.
    // Add the row to the DataRowCollection.
    myTable.Rows.Add(newRow);
 }

[C++] 
private:
void ShowRows(DataTable* myTable){
    // Print the number of rows in the collection.
    Console::WriteLine(myTable->Rows->Count);
    // Print the value of columns 1 in each row
    System::Collections::IEnumerator* myEnum = myTable->Rows->GetEnumerator();
    while (myEnum->MoveNext())
    {
        DataRow* row = __try_cast<DataRow*>(myEnum->Current);
        Console::WriteLine(row->Item[1]);
    }
 }
 
 void AddRow(DataTable* myTable){
    DataRow* newRow;
    DataRowCollection* rc = myTable->Rows;
    // Instantiate a new row using the NewRow method.
    newRow = myTable->NewRow();
    // Insert code to fill the row with values.
    // Add the row to the DataRowCollection.
    myTable->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

Namespace: System.Data

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

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

See Also

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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.