O'Reilly logo

ADO.NET in a Nutshell by Bill Hamilton, Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required