C#将查询返回的数据(一行多列)怎么转换成一列多行dataTable?
发布网友
发布时间:2022-04-27 12:33
我来回答
共3个回答
懂视网
时间:2022-04-27 16:54
代码如下:
/// <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;
}
热心网友
时间:2022-04-27 14:02
DataTable实现行列转换的方法,具体实现方法如下:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable tt = GetCrossTable(CreateDT());
GridView1.DataSource = tt;
GridView1.DataBind();
}
}
//创建DataTable
protected 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;
}
}
热心网友
时间:2022-04-27 15:20
你用什么对象查询的?