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

摘要:
简介:假设有一个DataTable对象dt具有以下列名称:产品名称、数量、单价和金额,那么我们可以通过以下方式向其添加一个合计行,以绑定到DataGrid对象doublesumquantity=0;双召唤=0;对于(inti=0;i˂dt.Rrows.Count;i++){sumquantity+=Convert.ToDou

引言:

假设存在一个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;

            }
        }

结果如下图:

免责声明:文章转载自《怎么样datatable表中增加一行合计行?》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Java性能优化之String字符串优化GVim上的vim-plug基础教程下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

db2数据库基本添加删除表字段总结

1.添加字段 alter table [table_name] add [column_name] [column_type] 2.更改字段类型 alter table [table_name] alter column [column_name] set data type [column_type] 注意: 更改字段类型是有限制的,如将字段改为比之前类...

element-ui的table,切换左侧树数据替换后高度变小问题

页面结构为左侧是树,右侧是表格,当切换左侧树时候,右侧表格数据改变,但是表格高度会变小,网上找的两种解决方法 方法一: 在表格数据改变后执行: this.$nextTick(()=>{ 表格加上ref='table' this.$refs.table.doLayout() }) 方法二: 1.固定表头高度 2.body_wrapper使用calc...

关于layui数据表格的各种事件

table.on('tool(demo)', function(obj){});监听工具条事件,tool 是工具条事件名,demo 是 table 原始容器的属性 lay-filter="对应的值"; table.on('toolbar(demo)', function(obj){});监听头部工具条事件,toolbar 是工具条事件名,demo 是 ta...

第4章 数据库安全性 | 数据库知识点整理

第4章 数据库安全性 了解 计算机系统安全性问题 数据库安全性问题 威胁数据库安全性因素 掌握 TCSEC和CC标准的主要内容 C2级DBMS、B1级DBMS的主要特征DBMS提供的安全措施 用户身份鉴别、自主存取控制、强制存取控制技术 视图技术和审计技术 数据加密存储和加密传输 使用SQL语言中的GRANT语句和REVOKE语句来实现自主存...

Unity多语言本地化改进版

简介 之前捣鼓过一个通过csv配置游戏多语言支持的小工具,但是发现使用过程中,通过notepad++去进行转码很不方便,并且直接将配置的csv不加密的放在游戏中心里感觉不是很踏实 于是乎~~ 新的方案 1.在PC/MAC平台上解析多语言配置,也就是editor运行环境中解析csv或者excel 2.通过在Editor运行过程中生成多个语言对象,然后序列化并...

浅析Lua中table的遍历

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://rangercyh.blog.51cto.com/1444712/1032925 当我在工作中使用lua进行开发时,发现在lua中有4种方式遍历一个table,当然,从本质上来说其实都一样,只是形式不同,这四种方式分别是: forke...