Datarow.itemarray Property
Gets or sets all values for 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 ten 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:
Exception Type |
conditions |
ArgumentException |
The array is larger than the number of columns in the table. |
InvalidCastException |
The values in the array do not match the DataType in their corresponding DataColumn. |
ConstraintException |
edit destroys the constraint. |
Readonlyexception |
The edit attempted to change the value of a read-only column. |
NoNullAllowedException |
The edit attempted to place a null value in a column with the AllowDBNull of false for the DataColumn object. |
Deletedrowinaccessibleexception |
The row has been deleted. |
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.readonly (datacolumn.columnname);} if (DataColumnChangeEventArgs! = null) {datacolumnchangeeventargs.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 ();d atacolumn[proposedrecordno] = obj;} catch (Exception e) {if (ADP. Iscatchableorsecurityexceptiontype (e) && flag) {this. CancelEdit ();} throw;} This. Lastchangedcolumn = Datacolumn;if (DataColumnChangeEventArgs! = null) {this._table. Oncolumnchanged (DataColumnChangeEventArgs);}}} This. EndEdit ();}}
The above is the content of C # Datarow.itemarray property, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!