Export (0) Print
Expand All

DataRow.ItemArray Property

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

[Visual Basic]
Public Property ItemArray As Object ()
[C#]
public object[] ItemArray {get; set;}
[C++]
public: __property Object* get_ItemArray();
public: __property void set_ItemArray(Object* __gc[]);
[JScript]
public function get ItemArray() : Object[];
public function set ItemArray(Object[]);

Property Value

An array of type Object.

Exceptions

Exception Type Condition
ArgumentException The array is larger than the number of columns in the table.
InvalidCastException A value in the array doesn't 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 the DataColumn object's AllowDBNull is false.
DeletedRowInaccessibleException The row has been deleted.

Remarks

If a DataColumn has its DefaultValue property set, pass a null reference (Nothing in Visual Basic) in the array to set the default value for that column. Similarly, if a column has its AutoIncrement property set to true, pass a null reference (Nothing) in the array to set the automatically generated value for the row.

An exception can occur if a user generates an exception in the ColumnChanging event, or the RowChanging event.

Example

[Visual Basic, C#, C++] 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 dr As DataRow
    ' Declare the array variable.
    Dim myArray(1) As Object
    ' Create 10 new rows and add to DataRowCollection.
    Dim i As Integer
    For i = 0 to 9
       myArray(0) = DBNull.Value
       myArray(1)= "item " & i.ToString()
       dr = dt.NewRow()
       dr.ItemArray = myArray
       dt.Rows.Add(dr)
    Next
    PrintTable(dt)
 End Sub
 
 Private Function MakeTableWithAutoIncrement() As DataTable
    ' Make a table with one AutoIncrement column.
    Dim myTable As DataTable = New DataTable("myTable")
    Dim dcID As DataColumn = New DataColumn("id", Type.GetType("System.Int32"))
    dcID.AutoIncrement = True
    dcID.AutoIncrementSeed = 10
    myTable.Columns.Add (dcID)
    
    Dim dcFirstName As DataColumn = New DataColumn("Item", Type.GetType("System.String"))
    myTable.Columns.Add(dcFirstName)
    MakeTableWithAutoIncrement = myTable
 End Function
 
 Private Sub PrintTable(myTable As DataTable)
    Dim myRow As DataRow
    Dim myColumn As DataColumn
    For Each myRow in myTable.Rows
       For Each myColumn in myTable.Columns
          Console.WriteLine(myRow(myColumn))
       Next
    Next
 End Sub

[C#] 
private void CreateRowsWithItemArray(){
    // Make a DataTable using the function below.
    DataTable dt = MakeTableWithAutoIncrement();
    DataRow dr;
    // Declare the array variable.
    object [] myArray = new object[2];
    // Create 10 new rows and add to DataRowCollection.
    for(int i = 0; i <10; i++){
       myArray[0]=null;
       myArray[1]= "item " + i;
       dr = dt.NewRow();
       dr.ItemArray = myArray;
       dt.Rows.Add(dr);
    }
    PrintTable(dt);
 }
 
 private DataTable MakeTableWithAutoIncrement(){
    // Make a table with one AutoIncrement column.
    DataTable myTable = new DataTable("myTable");
    DataColumn dcID = new DataColumn("id", Type.GetType("System.Int32"));
    dcID.AutoIncrement = true;
    dcID.AutoIncrementSeed = 10;
    myTable.Columns.Add(dcID);
    
    DataColumn dcFirstName = new DataColumn("Item", Type.GetType("System.String"));
    myTable.Columns.Add(dcFirstName);
    return myTable;
 }
 
 private void PrintTable(DataTable myTable){
    foreach(DataRow myRow in myTable.Rows){
       foreach(DataColumn myColumn in myTable.Columns){
          Console.WriteLine(myRow[myColumn]);
       }
    }
 }

[C++] 
private:
void CreateRowsWithItemArray(){
    // Make a DataTable using the function below.
    DataTable* dt = MakeTableWithAutoIncrement();
    DataRow* dr;
    // Declare the array variable.
    Object* myArray[] = new Object*[2];
    // Create 10 new rows and add to DataRowCollection.
    for(int i = 0; i <10; i++){
       myArray[0]=0;
       myArray[1]= String::Format( S"item {0}", __box(i));
       dr = dt->NewRow();
       dr->ItemArray = myArray;
       dt->Rows->Add(dr);
    }
    PrintTable(dt);
 }
 
 DataTable* MakeTableWithAutoIncrement(){
    // Make a table with one AutoIncrement column.
    DataTable* myTable = new DataTable(S"myTable");
    DataColumn* dcID = new DataColumn(S"id", Type::GetType(S"System.Int32"));
    dcID->AutoIncrement = true;
    dcID->AutoIncrementSeed = 10;
    myTable->Columns->Add(dcID);
    
    DataColumn* dcFirstName = new DataColumn(S"Item", Type::GetType(S"System.String"));
    myTable->Columns->Add(dcFirstName);
    return myTable;
 }
 
 void PrintTable(DataTable* myTable){
    System::Collections::IEnumerator* myEnum = myTable->Rows->GetEnumerator();
    while (myEnum->MoveNext())
    {
       DataRow* myRow = __try_cast<DataRow*>(myEnum->Current);
       System::Collections::IEnumerator* myEnum1 = myTable->Columns->GetEnumerator();
       while (myEnum1->MoveNext())
       {
          DataColumn* myColumn = __try_cast<DataColumn*>(myEnum1->Current);
          Console::WriteLine(myRow->Item[myColumn]);
       }
    }
 }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

DataRow Class | DataRow Members | System.Data Namespace | AcceptChanges | AutoIncrement | DataColumn

Show:
© 2014 Microsoft