System.Data Namespace DataRow Class
Sets or retrieves all of the values for this row through an array.
[ VB ]
Public Property ItemArray As Object ( )
[ C# ]
public object [ ] ItemArray {get; set;}
[ C++ ]
public: __property Object* get_ItemArray ( );
public: __property void set_ItemArray ( Object* [ ] );
[ JScript ]
public function get ItemArray ( ) : Object [ ];
public function set ItemArray ( Object [ ] );
An array of type Object.
If a DataColumn has its DefaultValue property set, pass a null reference 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 throws an exception 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 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 ) {
Response.Write ( myRow [ myColumn ] );
}
}
}
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 ) = null
myArray ( 1 ) = "item " + i
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
Emd Function
Private Sub PrintTable ( DataTable myTable )
Dim myRow As DataRow
Dim myColumn As DataColumn
For Each myRow in myTable.Rows
For Each myColumn in myTable.Columns
Response.Write ( myRow [ myColumn ] )
Next
Next
End Sub |
|
C# |
VB |
DataRow Members AcceptChanges AutoIncrement DataColumn