25.3 Collections Reference


Object[] itemArray = DataRow.ItemArray;

Gets or sets the values for the columns in the row as an object array.


The following example demonstrates how to use the ItemArray collection property to set values for the columns in a row:

// 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});

// add a row using ItemArray
object[] rowArray = new object[] {1, "Widget"};
DataRow row = dt.NewRow();
row.ItemArray = rowArray;

// get the data for the row using the ItemArray property
object[] row = dt.Rows[0].ItemArray; 


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 the column. The value for AutoIncrement columns should also be set to null in the ItemArray.

    Part I: ADO.NET Tutorial
    Part II: ADO.NET Core Classes
    Part III: API Quick Reference