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.