Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
DataRow Class
 ItemArray Property

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
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)
Visual Basic (Declaration)
Public Property ItemArray As Object()
Visual Basic (Usage)
Dim instance As DataRow
Dim value As Object()

value = instance.ItemArray

instance.ItemArray = value
C#
public Object[] ItemArray { get; set; }
Visual C++
public:
property array<Object^>^ ItemArray {
    array<Object^>^ get ();
    void set (array<Object^>^ value);
}
JScript
public function get ItemArray () : Object[]
public function set ItemArray (value : Object[])

Property Value

Type: array<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 nullNothingnullptra null reference (Nothing in Visual Basic) 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.

Visual Basic
Private Sub CreateRowsWithItemArray()
    ' Make a DataTable using the function below.
    Dim dt As DataTable = MakeTableWithAutoIncrement()
    Dim relation As DataRow

    ' Declare the array variable.
    Dim rowArray(1) As Object

    ' Create 10 new rows and add to DataRowCollection.
    Dim i As Integer
    For i = 0 to 9
       rowArray(0) = DBNull.Value
       rowArray(1)= "item " & i.ToString()
       relation = dt.NewRow()
       relation.ItemArray = rowArray
       dt.Rows.Add(relation)
    Next
    PrintTable(dt)
End Sub

Private Function MakeTableWithAutoIncrement() As DataTable
    ' Make a table with one AutoIncrement column.
    Dim table As DataTable = New DataTable("table")
    Dim idColumn As DataColumn = New DataColumn("id", _
        Type.GetType("System.Int32"))
    idColumn.AutoIncrement = True
    idColumn.AutoIncrementSeed = 10
    table.Columns.Add (idColumn)

    Dim firstNameColumn As DataColumn = New DataColumn( _
        "Item", Type.GetType("System.String"))
    table.Columns.Add(firstNameColumn)
    MakeTableWithAutoIncrement = table
End Function

Private Sub PrintTable(table As DataTable)
    Dim row As DataRow
    Dim column As DataColumn
    For Each row in table.Rows
       For Each column in table.Columns
          Console.WriteLine(row(column))
       Next
    Next
End Sub

C#
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]);
        }
    }
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker