码迷,mamicode.com
首页 > Windows程序 > 详细

C# DataTable数据增加小计合计列

时间:2020-03-18 11:38:05      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:string   nbsp   array   显示   tle   数组   bnu   turn   long   

/// <summary>
        /// 分类小计,并有合计行
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="sColHeJi">显示合计小计的字段</param>
        /// <param name="nColHeJi">需要显示‘合计’字段的列</param>
        /// <param name="colsHeJi">需要合计的列</param>
        public DataRow dbDataTableSubSumRowsWithColList(DataTable dt, string sColHeJi, string[] colsGroup, string[] colsHeJi)
        {
            DataRow dr = dt.NewRow();
            dr[sColHeJi] = "合计";
            dt.Rows.Add(dr);
            //初始化合计数组
            decimal[] arrDec = new decimal[colsHeJi.Length];
            for (int i = 0; i < colsHeJi.Length; i++)
            {
                arrDec[i] = decimal.Zero;
            }
            //合计
            for (int i = 0; i < dt.Rows.Count - 1; i++)
            {
                for (int j = 0; j < colsHeJi.Length; j++)
                {
                    string cName = colsHeJi[j];
                    if (Convert.IsDBNull(dt.Rows[i][cName])) continue;
                    arrDec[j] += Convert.ToDecimal(dt.Rows[i][cName]);//Tools.toDec(dt.Rows[i][cName] + "");
                }
            }
            for (int i = 0; i < colsHeJi.Length; i++)
            {
                string cName = colsHeJi[i];
                if (arrDec[i] == decimal.Zero) dr[cName] = DBNull.Value;
                else dr[cName] = arrDec[i];
            }
            if (dt.Rows.Count <= 1) return dr;
            //小计
            string sRate = "";
            int currSubSumCol = dt.Rows.Count - 2;
            System.Collections.ArrayList ar = new System.Collections.ArrayList();
            for (int i = dt.Rows.Count - 2; i >= 0; i--)
            {
                string currRate = dt.Rows[i]["belongedProjectTitle"] + "";
                if (sRate != currRate)
                {
                    if (i != dt.Rows.Count - 2)
                    {
                        dr = dt.NewRow();
                        dr[sColHeJi] = "小计";
                        for (int j = 0; j < colsHeJi.Length; j++)
                        {
                            string cName = colsHeJi[j];
                            if (arrDec[j] == decimal.Zero) dr[cName] = DBNull.Value;
                            else dr[cName] = arrDec[j];
                        }
                        dt.Rows.InsertAt(dr, currSubSumCol + 1);
                    }
                    currSubSumCol = i;
                    sRate = currRate;
                    for (int j = 0; j < colsHeJi.Length; j++)
                    { //归零
                        arrDec[j] = decimal.Zero;
                    }
                }
                for (int j = 0; j < colsHeJi.Length; j++)
                {
                    string cName = colsHeJi[j];
                    if (Convert.IsDBNull(dt.Rows[i][cName])) continue;
                    arrDec[j] += Convert.ToDecimal(dt.Rows[i][cName]);
                }
                if (i == 0)
                {
                    dr = dt.NewRow();
                    dr[sColHeJi] = "小计";
                    for (int j = 0; j < colsHeJi.Length; j++)
                    {
                        string cName = colsHeJi[j];
                        if (arrDec[j] == decimal.Zero) dr[cName] = DBNull.Value;
                        else dr[cName] = arrDec[j];
                    }
                    dt.Rows.InsertAt(dr, currSubSumCol + 1);
                }
            }

            return dr;
        }

 

C# DataTable数据增加小计合计列

标签:string   nbsp   array   显示   tle   数组   bnu   turn   long   

原文地址:https://www.cnblogs.com/otsf/p/12515892.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!