Datarow.itemarray Property
Gets or sets all the values of this row through an array.
Namespaces: System.Data
Assembly: System.Data (in system.data.dll)
code example:
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 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]); }
}
}
Abnormal:
This paper url:http://www.bianceng.cn/programming/csharp/201410/45585.htm
Datarow.itemarray Property Source code implementation:
Public object[] ItemArray {get {int defaultrecord = this.
Getdefaultrecord (); object[] array = new object[this._columns.
Count]; for (int i = 0; i < array. Length;
i++) {DataColumn DataColumn = this._columns[i];
Array[i] = Datacolumn[defaultrecord];
} return array;
set {if (value = = null) {throw exceptionbuilder.argumentnull ("ItemArray"); } if (This._columns. Count < value.
Length) {throw exceptionbuilder.valuearraylength ();
} DataColumnChangeEventArgs DataColumnChangeEventArgs = null; if (this._table.
needcolumnchangeevents) {DataColumnChangeEventArgs = new DataColumnChangeEventArgs (this); BOOL flag = this.
Begineditinternal (); for (int i = 0; i < value. Length; i++) {if (VALue[i]!= null) {DataColumn DataColumn = this._columns[i]; if ( -1l!= this.rowid && datacolumn.readonly) {throw Exceptionbuilder.rea
DOnly (Datacolumn.columnname); } if (DataColumnChangeEventArgs!= null) {Datacolumnchangeevent
Args.initializecolumnchangeevent (DataColumn, value[i]); This._table.
Oncolumnchanging (DataColumnChangeEventArgs); } if (datacolumn.table!= this._table) {throw exceptionbuilder. Columnnotinthetable (Datacolumn.columnname, this._table.
TableName); } if ( -1l!= this.rowid && datacolumn.readonly) {throw
Exceptionbuilder.readonly (Datacolumn.columnname); } if (This.temprecord = = 1)
{this.
Begineditinternal (); Object obj = (DataColumnChangeEventArgs!= null)?
Datacolumnchangeeventargs.proposedvalue:value[i];
if (obj = = null) {if (Datacolumn.isvaluetype) {
Throw Exceptionbuilder.cannotsettonull (DataColumn);
obj = DBNull.Value; try {int proposedrecordno = this.
Getproposedrecordno ();
Datacolumn[proposedrecordno] = obj; The catch (Exception e) {if (ADP). Iscatchableorsecurityexceptiontype (e) && flag) {this.
CancelEdit ();
} throw; } this.Lastchangedcolumn = DataColumn; if (DataColumnChangeEventArgs!= null) {this._table.
Oncolumnchanged (DataColumnChangeEventArgs); }}} this.
EndEdit (); }
}