ItemArray
Object[]itemArray
= DataRow.ItemArray; DataRow.ItemArray =itemArray
;
Gets or sets the values of the row using an object array. Each item
in the object array corresponds to a column in the
DataTable
.
The following example demonstrates how to get and set the values for
columns in a row using the ItemArray
property:
// create a table with two columns DataTable dt = new DataTable(); DataColumn colId = new DataColumn("ProductId", typeof(System.Int32)); DataColumn colDesc = new DataColumn("Description", typeof(System.String)); dt.Columns.AddRange(new DataColumn[] {colId, colDesc}); dt.Rows.Add(new object[] {1, "Widget"}); // get the data for the row using the ItemArray property object[] row = dt.Rows[0].ItemArray; // set the ProductId to be AutoIncrement colId.AutoIncrement = true; // pass null for the AutoIncrement value dt.Rows.Add(new object[] {null, "Thing"}); // let the description be null colDesc.AllowDBNull = true; // add a row with a null description, and AutoIncrement Id dt.Rows.Add(new object[] {null, null});
When the ItemArray
property is used, an attempt is
made to locate the row matching the primary key. If the row is found,
it is updated with the values in the ItemArray
;
otherwise, a new row is created.
Any columns with an array element set to null
are
set to the default value for a new column or the existing value for
the column if the row is being updated.
The value for AutoIncrement
columns should be set
to null
in the ItemArray
.