C # row to column

Source: Internet
Author: User

        <summary>///year row to///</summary>/<param name= "_outdatasource" &GT;&L            t;/param>//<returns></returns> Private DataTable RCC (DataTable _outdatasource) { Read the non-repeating date line from the DataTable to construct a column for the new datatable datatable distinct_date = _outdatasource.defaultview.totable (t            Rue, "year");            DataTable new_datatable = new DataTable ();            Add industry columns to the new table DataColumn New_d_col = new DataColumn (); New_d_col.            ColumnName = "Industry"; New_d_col.            Caption = "";            NEW_DATATABLE.COLUMNS.ADD (New_d_col);            StringBuilder str_sum = new StringBuilder (); Begins constructing a date column in a new table, foreach (DataRow Dr in Distinct_date.                Rows) {new_d_col = new DataColumn (); New_d_col.                DataType = typeof (decimal); New_d_col. ColumnName = dr["Year"].                ToString (); New_d_col. Caption = dr["Year"].        ToString ();        New_d_col.                DefaultValue = 0;                NEW_DATATABLE.COLUMNS.ADD (New_d_col); The purpose of this is to construct an expression annotation for the aggregate column as a calculated total//str_sum. Append ("+["). Append (dr["year"). ToString ()).            Append ("]");            ////Add the total column to the new table with the comments as calculated totals//new_d_col = new DataColumn (); New_d_col.            DataType = typeof (decimal); New_d_col.            ColumnName = "Sum"; New_d_col.            Caption = "Total"; New_d_col.            DefaultValue = 0; New_d_col. Expression = Str_sum. ToString ().            Substring (1);            NEW_DATATABLE.COLUMNS.ADD (New_d_col); /* To this new table has been built, the following starts to add data for the new table////////////////read from the original DataTable the industry as a key to construct a new table row DataTable Distinct_object = _outdat            ASource.DefaultView.ToTable (True, "industry");            Datarow[] DRS;            DataRow New_dr; foreach (DataRow Dr in Distinct_object.                Rows) {new_dr = New_datatable.newrow (); new_dr["Industry"] = dr["Industry"]. Tostring(); foreach (DataRow _dr in Distinct_date. Rows) {DRS = _outdatasource.select ("industry = '" + dr["industry"]. ToString () + "' and year = '" + _dr["year"].                    ToString () + "'"); if (Drs. Length! = 0) {new_dr[_dr["year"].                    ToString ()] = Math.Round (Convert.todecimal (drs[0]["Number of bars"]), 2);            }} new_DataTable.Rows.Add (NEW_DR);        } return new_datatable; }

Calling Method:

  DataTable newtable = new DataTable ();     NEWTABLE.COLUMNS.ADD ("Industry", typeof (String));             Industry name NEWTABLE.COLUMNS.ADD ("year", typeof (String));    Year NEWTABLE.COLUMNS.ADD ("number of Bars", typeof (String));            Number of int years = Convert.ToInt32 (DateTime.Now.ToString ("yyyy")); if (!string.            IsNullOrEmpty (CurrentYear)) {years = Convert.ToInt32 (currentyear); } for (var i = years; I >= years-9; i--) {DataTable dt = Getgroupindustrybyyear                (i.ToString ()); if (dt! = null && dt. Rows.Count > 0) {foreach (DataRow dr in DT).                        Rows) {DataRow n_dr = Newtable.newrow ();                        n_dr["Industry"] = convert.tostring (dr["Industrycategoryname"]);                        n_dr["Year"] = convert.tostring (i);        n_dr["Number of bars"] = convert.tostring (dr["Count"]);                NEWTABLE.ROWS.ADD (N_DR); }}} DataTable Newdt = RCC (newtable);

C # row to column

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.