前几天写的代码,有错误和不周希望大家直接指出来,谢谢了。
/// <summary>
/// 数据表转换为交叉表
/// </summary>
/// <param name="DT">数据表</param>
/// <param name="RowTitle">数据表中的字段名成,每一行的标题,也为左标题</param>
/// <param name="ColumnTitle">数据表中的字段名成,每一列的标题,也为上标题</param>
/// <param name="Context">数据表中的字段名成,汇总字段,必须为数字型</param>
/// <param name="DGV">DataGridView控件,用来显示</param>
/// <param name="IsSum">是否显示合计</param>
/// <param name="StrLeftTop">左上角文本</param>
public static void Cross_DGV(System.Data.DataTable _DataTable, string RowTitle, string ColumnTitle, string Context, System.Windows.Forms.DataGridView DGV, bool IsSum, string StrLeftTop)
{
DGV.Rows.Clear();
DGV.Columns.Clear();
foreach (System.Data.DataRow DR in _DataTable.Rows)
{
int IndexRow = -1;
int IndexColumn = -1;
#region 确定索引
if (0 == DGV.Rows.Count)
{
DGV.Columns.Add("TitleDGVTB",StrLeftTop);
DGV.Columns.Add("DGVTB0", DR[ColumnTitle].ToString());
DGV.Rows.Add();
DGV.Rows[0].Cells[0].Value = DR[RowTitle].ToString();
IndexRow = 0;
IndexColumn = 1;
}
else
{
foreach (DataGridViewRow DGVR in DGV.Rows)
{
if (null != DGVR.Cells[0].Value && DGVR.Cells[0].Value.ToString() == DR[RowTitle].ToString())
{
IndexRow = DGV.Rows.IndexOf(DGVR);
break;
}
}
if (-1 == IndexRow)
{
DGV.Rows.Add();
IndexRow = DGV.Rows.Count - 1;
DGV.Rows[IndexRow].Cells[0].Value = DR[RowTitle].ToString();
}
foreach (DataGridViewColumn DGVC in DGV.Columns)
{
if (DGVC.HeaderText == DR[ColumnTitle].ToString())
{
IndexColumn = DGV.Columns.IndexOf(DGVC);
break;
}
}
if (-1 == IndexColumn)
{
IndexColumn = DGV.Columns.Count;
DGV.Columns.Add("DGVTB" + IndexColumn.ToString(), DR[ColumnTitle].ToString());
}
}
#endregion
#region 赋值
if (null == DGV.Rows[IndexRow].Cells[IndexColumn].Value)
DGV.Rows[IndexRow].Cells[IndexColumn].Value = DR[Context];
else
DGV.Rows[IndexRow].Cells[IndexColumn].Value = decimal.Parse(DGV.Rows[IndexRow].Cells[IndexColumn].Value.ToString()) + decimal.Parse(DR[Context].ToString());
#endregion
}
#region 增加合计
if (IsSum)
{
DGV.Columns.Add("DGVTBSum", "合计");
foreach (DataGridViewRow DGVR in DGV.Rows)
foreach (DataGridViewColumn DGVC in DGV.Columns)
if (0 < DGVC.Index && DGV.Columns.Count > DGVC.Index + 1)
{
if (null == DGV.Rows[DGVR.Index].Cells[DGV.Columns.Count - 1].Value)
DGV.Rows[DGVR.Index].Cells[DGV.Columns.Count - 1].Value = 0;
if (null != DGV.Rows[DGVR.Index].Cells[DGVC.Index].Value)
DGV.Rows[DGVR.Index].Cells[DGV.Columns.Count - 1].Value =
decimal.Parse(DGV.Rows[DGVR.Index].Cells[DGV.Columns.Count - 1].Value.ToString()) +
decimal.Parse(DGV.Rows[DGVR.Index].Cells[DGVC.Index].Value.ToString());
}
DGV.Rows.Add();
int IndexRowSum = DGV.Rows.Count - 1;
DGV.Rows[IndexRowSum].Cells[0].Value = "合计";
foreach (DataGridViewColumn DGVC in DGV.Columns)
foreach (DataGridViewRow DGVR in DGV.Rows)
if (0 < DGVC.Index && IndexRowSum > DGVR.Index)
{
if (null == DGV.Rows[IndexRowSum].Cells[DGVC.Index].Value)
DGV.Rows[IndexRowSum].Cells[DGVC.Index].Value = 0;
if (null != DGV.Rows[DGVR.Index].Cells[DGVC.Index].Value)
DGV.Rows[IndexRowSum].Cells[DGVC.Index].Value =
decimal.Parse(DGV.Rows[DGVR.Index].Cells[DGVC.Index].Value.ToString()) +
decimal.Parse(DGV.Rows[IndexRowSum].Cells[DGVC.Index].Value.ToString());
}
}
#endregion
}