Name

Rows

Synopsis


DataRowCollection drc = DataTable.Rows;

Accesses the DataRowCollection for the DataTable, providing access to the DataRow objects belonging to the DataTable. The Rows property can be used to add, remove, and examine the DataRow objects in a DataTable.

Examples

There are two methods that can add a row to a table. The Add( ) method takes either a DataRow argument or an object array of columns of the row to be added:

DataTable dt = new DataTable("MyTable");

dt.Columns.Add("Column1", typeof(System.Int32));

dt.Columns.Add("Column2", typeof(System.String));



DataRow newRow = dt.NewRow();

newRow["Column1"] = 1;

newRow["Column2"] = "DataRow 1";



// add a row using a reference to a DataRow

dt.Rows.Add(newRow);



// add and create a DataRow in one statement

dt.Rows.Add(new Object[] {2, "DataRow 2"});

A DataRow can also be inserted at a specific point in the DataRowCollection using the InsertAt( ) method, which in addition to a reference to a DataRow, takes an argument specifying the zero-based index at which to insert the row.

// create a new row

DataRow row = dt.NewRow();

row.ItemArray = new Object[] {1, "DataRow 1"};



// insert a new row as the first item of the collection

dt.Rows.InsertAt(row,0);

The Contains( ) method returns a value that indicates whether the primary key exists in the collection of rows. The method has two overloads taking an object or an array of objects allowing primary keys based on one or more columns to be examined.

// look for a primary key that is based on a single column

bool exists = dt.Rows.Contains("PK Value 1");



// look for a primary key that is based on multiple columns

bool exists = dt.Rows.Contains(new Object[] {"PK Field1 Value",

    "PK Field2 Value"});

The Find( ) method is the second method available to locate a row based on the primary key. The Find( ) method differs from the Contains( ) method in that it returns the matching row rather than just indicating if a matching row exists. Like the Contains( ) method, the Find( ) method has two overloads taking an object or an array of objects allowing rows with primary keys based on a single or multiple columns to be returned. A null reference is returned if a matching row isn’t found.

// get the row for a primary key that is based on a single column

DataRow row = dt.Rows.Find("PK Value 1");



// get the row for a primary key that is based on multiple columns

DataRow row = dt.Rows.Find(new Object[] {"PK Field1 Value",

    "PK Field2 Value"});

The Remove( ) method removes a row specified by a DataRow argument from the collection:

// remove the row matching the primary key value, if found 

DataRow row = dt.Rows.Find("PK Value 1");

if(row != null)

    dt.Rows.Remove(row);

The RemoveAt( ) method removes the row specified by a zero-based index argument from the collection. If there no row at the index, an IndexOutOfRangeException is raised.

// remove the first row from the collection

dt.Rows.RemoveAt(0);

The Clear( ) method removes all rows from the collection:

// remove all rows from the table

dt.Rows.Clear();

Notes

The DataRowCollection class derives its standard functionality from the InternalDataCollectionBase class from which it inherits.

The commonly used public properties of the DataRowCollection are listed and described in Table 23-11.

The commonly used public methods of the DataRowCollection are listed and described in Table 23-12.