A detailed explanation of C # Datarow.itemarray attributes

Source: Internet
Author: User

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 (); }  
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.