如何对数据表列求和并得到差异 [英] how to sum data table column and get the diffrence
本文介绍了如何对数据表列求和并得到差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好..
i需要制作一个这样的数据表:
hello ..
i need to make a data table like that:
Subjects old new diff
Sub_1 10 50 40
Sub_2 30 10 -20
total 40 60 20
这是一个部分代码
this is a part of code
DataTable subjects = new DataTable();
subjects.Columns.Add("Subjects");
subjects.Columns.Add("old");
subjects.Columns.Add("new");
subjects.Columns.Add("diff");
subjects.Rows.Add("Sub_1", sub1.Old , sub1.New , (sub1.New - sub1.Old));
subjects.Rows.Add("Sub_2", sub2.Old , sub2.New , (sub2.New - sub2.Old));
subjects.Rows.Add("Total", .. total of above .. , .. total of above .., .. total of above ..);
所以我需要问一下如何计算最后一列的总价值(总计),还有其他方法来计算第四列库存(3rdcol - 2 2nd col)
so i need to ask how to calculate the total value of last column ( Total) , and is there is any other way to calculate the 4th coulmn ( 3rdcol - 2 2nd col )
推荐答案
添加一个对当前行中的总计进行操作的计算列非常简单:您不必自己进行数学运算,表格可以为您完成:
Adding a calculated column which operates on the totals in the current row is easy: You don't have to do the math yourself, the table can do that for you:
DataTable subjects = new DataTable();
subjects.Columns.Add("Subjects");
subjects.Columns.Add("old", typeof(int));
subjects.Columns.Add("new", typeof(int));
subjects.Columns.Add("diff", typeof(int), "new-old");
subjects.Rows.Add("Sub_1", 50, 60);
subjects.Rows.Add("Sub_2", 100, 46);
但是没有自动生成总行AFAIK的方法。
你可以手动完成:
But there is no automatic way to generate a totals row AFAIK.
You can do it manually:
subjects.Rows.Add("TOTAL", subjects.Compute("Sum(old)", ""), subjects.Compute("Sum(new)", ""), subjects.Compute("Sum(diff)", ""));
但是赢了; t自动更新 - 每次单元格更改或添加/删除行时都必须重新运行计算。
But that won;t update itself automaticaaly - you will have to re-run the Computes each time a cell changes or a row is added / deleted.
这篇关于如何对数据表列求和并得到差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文