Export (0) Print
Expand All
2 out of 5 rated this helpful - Rate this topic

DataRow.ItemArray Property

Gets or sets all the values for this row through an array.

Namespace:  System.Data
Assembly:  System.Data (in System.Data.dll)
public Object[] ItemArray { get; set; }

Property Value

Type: System.Object[]
An array of type Object.
ExceptionCondition
ArgumentException

The array is larger than the number of columns in the table.

InvalidCastException

A value in the array does not match its DataType in its respective DataColumn.

ConstraintException

An edit broke a constraint.

ReadOnlyException

An edit tried to change the value of a read-only column.

NoNullAllowedException

An edit tried to put a null value in a column where AllowDBNull of the DataColumn object is false.

DeletedRowInaccessibleException

The row has been deleted.

You can use this property to set or get values for this row through an array. If you use this property to set values, the array must have the same size and ordering as the column collection. Passing null in the ItemArray indicates that no value was specified.

Users can generate exceptions in the ColumnChanging event or the RowChanging event.

The following examples show how to get and set values using the ItemArray property.

private void CreateRowsWithItemArray()
{
    // Make a DataTable using the function below.
    DataTable dt = MakeTableWithAutoIncrement();
    DataRow relation;
    // Declare the array variable. 
    object [] rowArray = new object[2];
    // Create 10 new rows and add to DataRowCollection. 
    for(int i = 0; i <10; i++)
    {
        rowArray[0]=null;
        rowArray[1]= "item " + i;
        relation = dt.NewRow();
        relation.ItemArray = rowArray;
        dt.Rows.Add(relation);
    }
    PrintTable(dt);
}

private DataTable MakeTableWithAutoIncrement()
{
    // Make a table with one AutoIncrement column.
    DataTable table = new DataTable("table");
    DataColumn idColumn = new DataColumn("id", 
        Type.GetType("System.Int32"));
    idColumn.AutoIncrement = true;
    idColumn.AutoIncrementSeed = 10;
    table.Columns.Add(idColumn);

    DataColumn firstNameColumn = new DataColumn("Item", 
        Type.GetType("System.String"));
    table.Columns.Add(firstNameColumn);
    return table;
}

private void PrintTable(DataTable table)
{
    foreach(DataRow row in table.Rows)
    {
        foreach(DataColumn column in table.Columns)
        {
            Console.WriteLine(row[column]);
        }
    }
}

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.