C#中DataTable實現行列轉換的方法

來源:互聯網
上載者:User
本文執行個體講述了C#中DataTable實現行列轉換的方法。分享給大家供大家參考。具體實現方法如下:

protected void Page_Load(object sender, EventArgs e){  if (!IsPostBack)  {   DataTable tt = GetCrossTable(CreateDT());   GridView1.DataSource = tt;   GridView1.DataBind();  }}//建立DataTableprotected DataTable CreateDT(){  DataTable tblDatas = new DataTable("Datas");  //資料列  tblDatas.Columns.Add("姓名", Type.GetType("System.String"));  tblDatas.Columns.Add("科目", Type.GetType("System.String"));  tblDatas.Columns.Add("分數", Type.GetType("System.Int32"));  tblDatas.Rows.Add(new object[] { "張三", "語文", 89 });  tblDatas.Rows.Add(new object[] { "張三", "數學", 90 });  tblDatas.Rows.Add(new object[] { "張三", "英語", 79 });  tblDatas.Rows.Add(new object[] { "張三", "地理", 70 });  tblDatas.Rows.Add(new object[] { "張三", "生物", 95 });  tblDatas.Rows.Add(new object[] { "李四", "語文", 87 });  tblDatas.Rows.Add(new object[] { "李四", "英語", 86 });  tblDatas.Rows.Add(new object[] { "李四", "地理", 82 });  tblDatas.Rows.Add(new object[] { "王五", "語文", 81 });  tblDatas.Rows.Add(new object[] { "王五", "數學", 70 });  tblDatas.Rows.Add(new object[] { "王五", "英語", 88 });  tblDatas.Rows.Add(new object[] { "王五", "生物", 96 });  return tblDatas;}/// <summary>/// 將DataTable的第二列的值轉化為列//(即將原來的行表,轉化成交叉表,沒有對應值則預設"0")/// </summary>/// <param name="dt">必須三列,第三列為值</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;  }}

希望本文所述對大家的C#程式設計有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.