A
DataRow
object represents a row of data in the
table stored in a collection of columns. Rows belonging to the
DataTable
are stored as DataRow
objects in a DataRowCollection
object and are
accessed through the Rows
property of the
DataTable
. The Rows
property
accesses methods and properties that can add, remove, and examine the
DataRow
objects in a DataTable
.
This section examines some of the methods and properties of the
DataRowCollection
.
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"].Value = 1; newrow["Column2"].Value = "DataRow 1"; // add a row using a reference to a DataRow dt.Rows.Add(dr); // add and create a DataRow in one statement dt.Rows.Add(new Object[] {2, "DataRow 2"});
Additionally, a DataRow
can 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);
There are two methods that can examine and locate rows within the
DataRow
collection. The Contains( )
method returns a value indicating
whether the primary key exists in the collection of rows. The method
has two overloads that take an object or an array of objects and
allow primary keys based on one or more columns to be examined.
// look for a primary key that is based on a single column Boolean exists = dt.Rows.Contains("PK Value 1"); // look for a primary key that is based on multiple columns Boolean exists = dt.Rows.Contains(new Object[] {"PK Field1 Value", "PK Field2 Value"});
The Find( )
method is the second method that can
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 was found. Like the Contains( )
method, the
Find( )
method has two overloads that take an
object or an array of objects and allow primary keys based on one or
more columns to be examined. 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( )
, RemoveAt( )
, and
Clear( )
methods remove rows from the
DataTable
. The Remove( )
method
takes a DataRow
argument and removes that row 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
takes an argument that specifies the index within the collection at
which to remove the row:
// remove the first row from the collection dt.Rows.RemoveAt(0);
Finally, the Clear( )
method removes all rows from the
table:
// remove all rows from the table dt.Rows.Clear();
Rows are discussed in detail in Chapter 9.