DataTable 分页汇总

             //要分页的DataTable
            DataTable dt = new DataTable();

            //分页结果集
            DataTable dtRestult = dt.Clone();

            //分页用的临时dt
            DataTable dtTemp = dt.Clone();

            //每页显示的条数
            int i = 10;

            //分页后的页数
            int iPageCount = 0;

            //要计算的列名
            List<string> sComputer = new List<string>();

            foreach (DataColumn dc in dtTemp.Columns)
            {
                switch (dc.DataType.FullName)
                {
                    case "System.Int32":
                    case "System.Decimal":
                    case "System.Int64":
                        sComputer.Add(dc.ColumnName);
                        break;
                }
            }

            for (int j = 0; j < dt.Rows.Count; j++)
            {
                dtTemp.Rows.Add(dt.Rows[j].ItemArray);

                if ((j + 1) % i == 0 || j==dt.Rows.Count -1)
                {
                    DataRow drTemp = dtTemp.NewRow();

                    foreach (string s in sComputer)
                    {
                        object obj = dtTemp.Compute("Sum(" + s + ")","");
                      
                        drTemp[s] = obj;
                    }

                    //这里在实际用的时候换一下
                    drTemp[0] = " 合 计 ";

                    dtTemp.Rows.Add(drTemp);

                    dtRestult.Merge(dtTemp, true);

                    dtTemp = dt.Clone();

                    iPageCount++;
                }
            }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值