你的位置:首页 > ASP.net教程

[ASP.net教程]DataTable行转列


 /// <summary> /// DataTable行转列 /// </summary> /// <param name="dtable">需要转换的表</param> /// <param name="head">转换表表头对应旧表字段(小写)</param> /// <returns></returns> public static DataTable DataTableRowtoCon(DataTable dtable, string head) {  DataTable dt = new DataTable();  dt.Columns.Add("NumberID");  for (int i = 0; i < dtable.Rows.Count; i++)  {//设置表头  dt.Columns.Add(dtable.Rows[i][head].ToString());  }  for (int k = 0; k < dtable.Columns.Count; k++)  {  string temcol = dtable.Columns[k].ToString();  if (dtable.Columns[k].ToString().ToLower() != head)//过滤掉设置表头的列  {   DataRow new_dr = dt.NewRow();   new_dr[0] = dtable.Columns[k].ToString();   for (int j = 0; j < dtable.Rows.Count; j++)   {   string temp = dtable.Rows[j][k].ToString();   new_dr[j + 1] = (Object)temp;   }   dt.Rows.Add(new_dr);  }  }  return dt; }