怎么样datatable表中增加一行合计行?

引言:

假设存在一个DataTable对象dt,具有以下列名:产品名称productname,数量quantity,单价price,金额money,那么我们可通过下列方式给它添加合计行以绑定到DataGrid对象
double sumquantity=0;
double summoney=0;
for(int i=0;i<dt.Rows.Count;i++)
{
sumquantity += Convert.ToDouble(dt.Rows[i]["quantity"]);
summoney += Convert.ToDouble(dt.Rows[i]["money"]);
}
DataRow dr=dt.NewRow ();
dr["productname"]="合计";
dr["quantity"]=sumquantity;
dr["money"]=summoney;
dt.Rows.Add(dr);

扩展实例:

存在一个DataTable对象table,添加一行统计汇总信息

 void ucSearchXuHang1_SelectReportEvent(object sender, EventArgs e)
        {
            //查询数据
            gridView1.Columns.Clear();
            string where = sender as string;
            string ids = taskTree1.SelectedNodeIDs;
            if (ids.Length <= 0)
            {
                XtraMessageBox.Show("请在左边的树结构中选择要查询的任务!", "提示");
            }
            else
            {
                string whereIds = string.Format(" and cr.TaskRecordId in ({0}) ", ids);
                string sql = string.Empty;
                where += whereIds;
                sql = string.Format(@"select cr.CaseName as 用例名称,
case cr.TimeElapsed when -1 then 0 else cr.TimeElapsed  end as 用例用时,
cr.CaseStepCount as 用例步骤总数,
cr.FinishedStepCount as 完成步骤数,
case cr.ResultCode when 3 then '成功' when 4 then '失败' when 5 then '未知' when 6 then '取消'  else '错误' end as 执行结果
from CaseRecords cr,StepRecords sr
where sr.CaseRecordId=cr.RecordId and cr.ExecuteState=16 {0} ", where, ucSearchXuHang1.SelectNames);
                DataTable table = this.aReport.SqlServerReturnDataSet(sql).Tables[0];
                string sqlTo = string.Format(@"select(select COUNT(0) from CaseRecords as cr where cr.ResultCode=3 and cr.ExecuteState=16 {0}) as succeedNum,
(select COUNT(0) from CaseRecords as cr where cr.ResultCode<>3 and cr.ExecuteState=16 {0}) as failNum", where);
                DataTable successTable = this.aReport.SqlServerReturnDataSet(sqlTo).Tables[0];

                for (int i = 0; i < successTable.Columns.Count; i++)
                {
                    string str = successTable.Columns[i].ColumnName;
                    switch (str)
                    {
                        case "succeedNum": successNum = Convert.ToInt32(successTable.Rows[0][i]); break;
                        case "failNum": failNum = Convert.ToInt32(successTable.Rows[0][i]); break;
                    }
                }
                totalNum = successNum + failNum;
                if (totalNum > 0)
                {
                    double succeedRate = Math.Round((Convert.ToDouble(successNum) / totalNum) * 100, 2);
                    double failRate = Math.Round((Convert.ToDouble(failNum) / totalNum) * 100, 2);

                    strMsgSuccess = string.Format("成功:{0}条  {1}%", successNum, succeedRate);
                    strMsgFail = string.Format("失败:{0}条  {1}%", failNum, failRate);
                    strMsgTotal = string.Format("总共:{0}条", totalNum);
                }
                try
                {
                    double sumTimeElapsed = 0;
                    double sumCaseStepCount = 0;
                    double sumFinishedStepCount = 0;
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        sumTimeElapsed += Convert.ToDouble(table.Rows[i]["用例用时"]);
                        sumCaseStepCount += Convert.ToDouble(table.Rows[i]["用例步骤总数"]);
                        sumFinishedStepCount += Convert.ToDouble(table.Rows[i]["完成步骤数"]);
                    }
                    DataRow row = table.NewRow();
                    if (table != null)
                    {
                        row["用例名称"] = "总计";
                        row["用例用时"] = sumTimeElapsed;
                        row["用例步骤总数"] = sumCaseStepCount;
                        row["完成步骤数"] = sumFinishedStepCount;
                        row["执行结果"] = string.Format("{0},{1},{2}", strMsgSuccess, strMsgFail, strMsgTotal);
                    }
                    else
                    {
                        row["CaseName"] = "总计";
                        row["ResultCode"] = string.Format("成功:0条  0%,失败:0条  0%,总共:0条");
                    }
                    table.Rows.Add(row);

                }
                catch (Exception)
                {

                    throw;
                }
                XuHangData.DataSource = table;

            }
        }

结果如下图:

 

 

 

 

转载于:https://www.cnblogs.com/lqsilly/archive/2013/03/28/2987105.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值