原数据结构:
转换后数据结构:
当然可以移除不必要的列:如:FNetCostAmount ,移除方法很简单,
//参数肯定是数据源啦。
DataTable dt = RowToCol(newDt);
dt.Columns.Remove("FNetCostAmount");
private DataTable RowToCol(DataTable dataSource)
{
//读取所需列内所有行数据
DataTable newDataTable = dataSource;
//维度:1、列标题名 2、⒈行坐标 ⒉行内值
Dictionary<string, Dictionary<int, string>> myDictionary = new Dictionary<string, Dictionary<int, string>>();
for (int i = 0; i < newDataTable.Rows.Count; i++)
{
if (!myDictionary.ContainsKey(newDataTable.Rows[i]["FBrandGroupname"].ToString()))
{
Dictionary<int, string> myIndex = new Dictionary<int, string>();
myIndex.Add(i, newDataTable.Rows[i]["FNetCostAmount"].ToString());
myDictionary.Add(newDataTable.Rows[i]["FBrandGroupname"].ToString(), myIndex);
}
else
{
myDictionary[newDataTable.Rows[i]["FBrandGroupname"].ToString()].Add(i, newDataTable.Rows[i]["FNetCostAmount"].ToString());
}
}
newDataTable.Columns.Remove("FBrandGroupname");
foreach (string key in myDictionary.Keys)
{
newDataTable.Columns.Add(key);
foreach (KeyValuePair<int, string> item in myDictionary[key.ToString(CultureInfo.InvariantCulture)])
{
newDataTable.Rows[item.Key][key] = item.Value;
}
}
return newDataTable;
}
不懂的,自己打断点看数据格式。