A method for row and column transformation in a DataTable in C #

Source: Internet
Author: User
The example in this paper describes the method for the conversion of a DataTable in C #. Share to everyone for your reference. The implementation method is as follows:

protected void Page_Load (object sender, EventArgs e) {if (!   IsPostBack) {DataTable TT = getcrosstable (Createdt ());   Gridview1.datasource = TT;  Gridview1.databind ();  }}//Create datatableprotected DataTable Createdt () {datatable tbldatas = new DataTable ("Datas");  Data column TBLDATAS.COLUMNS.ADD ("Name", Type.GetType ("System.String"));  TBLDATAS.COLUMNS.ADD ("Subject", Type.GetType ("System.String"));  TBLDATAS.COLUMNS.ADD ("Score", Type.GetType ("System.Int32"));  TBLDATAS.ROWS.ADD (new object[] {"Zhang San", "Language", 89});  TBLDATAS.ROWS.ADD (new object[] {"Zhang San", "Mathematics", 90});  TBLDATAS.ROWS.ADD (new object[] {"Zhang San", "English", 79});  TBLDATAS.ROWS.ADD (new object[] {"Zhang San", "Geography", 70});  TBLDATAS.ROWS.ADD (new object[] {"Zhang San", "Mob", 95});  TBLDATAS.ROWS.ADD (new object[] {"John Doe", "Language", 87});  TBLDATAS.ROWS.ADD (new object[] {"John Doe", "English", 86});  TBLDATAS.ROWS.ADD (new object[] {"John Doe", "Geography", 82});  TBLDATAS.ROWS.ADD (new object[] {"Harry", "Language", 81});  TBLDATAS.ROWS.ADD (new object[] {"Harry", "Mathematics", 70}); TblDatas.Rows.Add(New object[] {"Harry", "English", 88});  TBLDATAS.ROWS.ADD (new object[] {"Harry", "Mob", 96}); return Tbldatas;} <summary>///converts the value of the second column of the DataTable to a column//(the original row table is converted to a crosstab, no corresponding value is default "0")///</summary>///<param name= "DT "> must be three columns, third column value </param>///<returns></returns>public static DataTable getcrosstable (DataTable DT) {if (dt = = NULL | | dt. Columns.count! = 3 | | Dt.  Rows.Count = = 0) {return dt;   } else {datatable result = new DataTable (); Result. Columns.Add (dt. Columns[0].   ColumnName); DataTable dtcolumns = dt. Defaultview.totable ("Dtcolumns", true, dt. COLUMNS[1].   ColumnName);    for (int i = 0; i < DtColumns.Rows.Count; i++) {string colname; if (Dtcolumns.rows[1][0] is DateTime) {colname = Convert.todatetime (Dtcolumns.rows[i][0]).    ToString (); } else {colname = dtcolumns.rows[i][0].    ToString (); } result.    Columns.Add (colname); Result. Columns[i + 1].   DefaultValue = "0"; } DataRow drnew = result.   NewRow (); DRNEW[0] = dt. ROWS[0][0]; String rowname = Drnew[0].   ToString (); foreach (DataRow dr in Dt. Rows) {string colname = dr[1].    ToString ();    Double dvalue = convert.todouble (dr[2]); if (Dr[0]. ToString ().    Equals (RowName, stringcomparison.currentcultureignorecase)) {Drnew[colname] = dvalue.tostring (); } else {result.     Rows.Add (drnew); Drnew = result.     NewRow ();     Drnew[0] = dr[0]; RowName = drnew[0].     ToString ();    Drnew[colname] = dvalue.tostring (); }} result.   Rows.Add (drnew);  return result; }}

Hopefully this article will help you with your C # programming.

Related Article

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.