数据表转换为交叉表

前几天写的代码,有错误和不周希望大家直接指出来,谢谢了。

        /// <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

        }
       

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值