大家是否愿意讨论数据汇总报表的技术:数据汇总和GridView多表头的生成
相关文章请参阅:
DataGrid 控件中的汇总行:http://www.microsoft.com/china/MSDN/library/data/dataAccess/Summary+Rows+in+DataGrid+Controls.rtf.mspx?mfr=true
PivotTable示例文件:
/Files/lovenets2008/PivotTableDemo.rar
其中用到的函数:
DataGrid 控件中的汇总行:http://www.microsoft.com/china/MSDN/library/data/dataAccess/Summary+Rows+in+DataGrid+Controls.rtf.mspx?mfr=true
PivotTable示例文件:
/Files/lovenets2008/PivotTableDemo.rar
其中用到的函数:
public static DataTable MakePivotFromTable_Demo(DataTable source, string columeTop, string columeLeft, string columeSum)
{
DataTable dest = new DataTable("Pivoted" + source.TableName);
// the remaining dest columns are from each source table row (1st column)
//for (int i = 1; i < source.Columns.Count - 1; i++)
//{
// dest.Columns.Add(source.Columns[i].ColumnName);
//}
//source.LoadDataRow(ll ,bbbb);
DataTable dtCols = new DataTable();
dtCols.Columns.Add(columeTop);
dtCols.Constraints.Add("key", dtCols.Columns[0], true);
foreach (DataRow r in source.Rows)
{
dtCols.LoadDataRow(r[columeTop].ToString().Split(",".ToCharArray()), true); // assign each row the Product name (r[0])
}
dest.Columns.Add(columeTop);//"汇总");
//dest.Columns.Add(columesSum);
foreach (DataRow r in dtCols.Rows)
{
dest.Columns.Add(r[columeTop].ToString()); // assign each row the Product name (r[0])
}
// now add one row to the dest table for each column in the source, except
// the first which is the Product, in our case
dest.Constraints.Add("key", dest.Columns[0], true);
for (int i = 0; i < source.Rows.Count; i++)
{
dest.LoadDataRow(source.Rows[i][columeLeft].ToString().Split(",".ToCharArray()), true);
//DataRow dr = dest.NewRow();
//dr[0] = source.Rows[i][columesLeft];
//try
//{
// dest.Rows.Add(dr);
//}
//catch (Exception) { }
}
//now move the source columns to their position in the dest row/cell matrix
//starting down the destination rows, and across the columns
for (int r = 0; r < dest.Rows.Count; r++)
{
for (int c = 1; c < dest.Columns.Count; c++)
{
float amounts = 0;
DataRow[] drs = source.Select(source.Columns[columeTop].ColumnName + "='" + dest.Columns[c].ColumnName
+ "' AND " + source.Columns[columeLeft].ColumnName + "='" + dest.Rows[r][0].ToString() + "'");
foreach (DataRow dr in drs)
{
amounts += float.Parse(dr[columeSum].ToString());
}
if (drs.Length > 0) dest.Rows[r][c] = amounts;
}
}
//dest.Select();
//DataTable dt = dest.Constraints.Add(;
//dest.Select("Select * ");
dest.AcceptChanges();
return dest;
}
{
DataTable dest = new DataTable("Pivoted" + source.TableName);
// the remaining dest columns are from each source table row (1st column)
//for (int i = 1; i < source.Columns.Count - 1; i++)
//{
// dest.Columns.Add(source.Columns[i].ColumnName);
//}
//source.LoadDataRow(ll ,bbbb);
DataTable dtCols = new DataTable();
dtCols.Columns.Add(columeTop);
dtCols.Constraints.Add("key", dtCols.Columns[0], true);
foreach (DataRow r in source.Rows)
{
dtCols.LoadDataRow(r[columeTop].ToString().Split(",".ToCharArray()), true); // assign each row the Product name (r[0])
}
dest.Columns.Add(columeTop);//"汇总");
//dest.Columns.Add(columesSum);
foreach (DataRow r in dtCols.Rows)
{
dest.Columns.Add(r[columeTop].ToString()); // assign each row the Product name (r[0])
}
// now add one row to the dest table for each column in the source, except
// the first which is the Product, in our case
dest.Constraints.Add("key", dest.Columns[0], true);
for (int i = 0; i < source.Rows.Count; i++)
{
dest.LoadDataRow(source.Rows[i][columeLeft].ToString().Split(",".ToCharArray()), true);
//DataRow dr = dest.NewRow();
//dr[0] = source.Rows[i][columesLeft];
//try
//{
// dest.Rows.Add(dr);
//}
//catch (Exception) { }
}
//now move the source columns to their position in the dest row/cell matrix
//starting down the destination rows, and across the columns
for (int r = 0; r < dest.Rows.Count; r++)
{
for (int c = 1; c < dest.Columns.Count; c++)
{
float amounts = 0;
DataRow[] drs = source.Select(source.Columns[columeTop].ColumnName + "='" + dest.Columns[c].ColumnName
+ "' AND " + source.Columns[columeLeft].ColumnName + "='" + dest.Rows[r][0].ToString() + "'");
foreach (DataRow dr in drs)
{
amounts += float.Parse(dr[columeSum].ToString());
}
if (drs.Length > 0) dest.Rows[r][c] = amounts;
}
}
//dest.Select();
//DataTable dt = dest.Constraints.Add(;
//dest.Select("Select * ");
dest.AcceptChanges();
return dest;
}
相关进展会陆续贴于此。