如何总结列的DataTable? [英] How to sum columns in a dataTable?
本文介绍了如何总结列的DataTable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我怎样才能在一个DataTable中所有列的总和?说我下面的表格了。如何计算总行?它应该很容易总计行添加到DataTable。
列的唯一身份命中sigups,等...
行
1 12 1 23
2 1 0 5
3 6 2 9
共有19 3 37
更新
我结束了这一点。这是我能得到工作的唯一的东西。
对于每一个山坳作为的DataColumn在TotalsTable.Columns
。如果山坳.DataType.Name =日期时间然后
计数=计数+ 1
继续
端如果
尺寸colTotal为双= 0
尺寸值作为双
的每一行作为的DataRow在TotalsTable.Rows
如果Double.TryParse(行(列),价值)然后
colTotal + = Double.Parse(行(COL ))
端如果
下一步
totalRow(计数)= colTotal
计数=计数+ 1
下一步
解决方案
试试这个:
DataTable的DT =新的DataTable();
INT总和= 0;
的foreach(在dt.Rows的DataRow DR)
{
的foreach(DataColumn的DC IN dt.Columns)
{
总和+ =(int)的博士[DC] ;
}
}
How can I get a sum for all the columns in a datatable? Say I had the following table. How can I calculate the "total" row? It should be easy to add total row to a datatable.
Columns hits uniques sigups, etc...
Rows
1 12 1 23
2 1 0 5
3 6 2 9
total 19 3 37
Update
I ended up with this. It was the only thing I could get to work.
For Each col As DataColumn In TotalsTable.Columns
If col.DataType.Name = "DateTime" Then
count = count + 1
Continue For
End If
Dim colTotal As Double = 0
Dim value As Double
For Each row As DataRow In TotalsTable.Rows
If Double.TryParse(row(col), value) Then
colTotal += Double.Parse(row(col))
End If
Next
totalRow(count) = colTotal
count = count + 1
Next
解决方案
Try this:
DataTable dt = new DataTable();
int sum = 0;
foreach (DataRow dr in dt.Rows)
{
foreach (DataColumn dc in dt.Columns)
{
sum += (int)dr[dc];
}
}
这篇关于如何总结列的DataTable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文