怎么样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;

            }
        }

结果如下图:

 

 

 

 

posted @ 2013-03-28 17:01  乡香田甜  阅读(2625)  评论(0编辑  收藏  举报