在GridView中转置列 [英] Transposing columns in GridView
本文介绍了在GridView中转置列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据表,其中包含如下数据.
I have a data table that contains the data like below.
我想像下面一样在我的网格视图中显示它.实际上,这是上表的转置,并添加了一个用于查看产品详细信息的附加行,这将是一个链接按钮.能否请您帮我如何使用C#在ASP.net中实现以下要求.
I want to display it in my Grid View like Following. It is actually the transpose of above table and one additional Row added for Viewing Product Details that will be a link button. Can you please help me how can I achieve the following requirement in ASP.net using C#.
非常感谢,阿瓦伊斯·阿夫扎尔(Awais Afzal).
Many Thanks, Awais Afzal.
推荐答案
假设您的表是 DataTable
,则可以使用此类扩展名对其重新排序:
Assuming that you table is a DataTable
, you could use such an extension to reorder it:
public static DataTable Pivot(this DataTable tbl)
{
var tblPivot = new DataTable();
tblPivot.Columns.Add(tbl.Columns[0].ColumnName);
for (int i = 1; i < tbl.Rows.Count; i++)
{
tblPivot.Columns.Add(Convert.ToString(i));
}
for (int col = 0; col < tbl.Columns.Count; col++)
{
var r = tblPivot.NewRow();
r[0] = tbl.Columns[col].ToString();
for (int j = 1; j < tbl.Rows.Count; j++)
r[j] = tbl.Rows[j][col];
tblPivot.Rows.Add(r);
}
return tblPivot;
}
并将其设置为新的 DataSource
:
dataGridView1.DataSource = oldDataTable.Pivot();
这篇关于在GridView中转置列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文