复制代码 代码如下:
/// <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;
}
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!