Are you saying you have a table with multiple numeric columns and you want to get the sum of those columns per row?
I am not sure i got the question.
Victor
2008-12-05 15:37:01
Are you saying you have a table with multiple numeric columns and you want to get the sum of those columns per row?
I am not sure i got the question.
From msdn:
If you must perform an operation on two or more columns, you should create a DataColumn, set its Expression property to an appropriate expression, and use an aggregate expression on the resulting column. In that case, given a DataColumn with the name "total", and the Expression property set to this:
"Quantity * UnitPrice"
LINQ to the rescue:
DataTable dt = WhateverCreatesDataTable();
DataRow dr = dt.Rows[0];
int sum = dt.Columns.Cast<DataColumn>().Sum(dc=>(int)dr[dc]);
For those still dragging their knuckles in the stone ages (aka pre-.Net 3.5 and LINQ):
DataTable dt = WhateverCreatesDataTable();
DataRow dr = dt.Rows[0];
int sum = 0;
foreach(DataColumn dc in dt.Columns)
sum += (int)dr[dc];
Since you want to solve this in 1.1 here is what you can do as a simple solution
DataColumn totalColumn = new DataColumn();
totalColumn.DataType = System.Type.GetType("System.Int32");
totalColumn.ColumnName = "Total";
totalColumn.Expression = "Product1 + Product2 + Product3 + Product4 + Product5";
// Populate and get the DataTable dt then add this computed column to this table
dt.Columns.Add(totalColumn);
//Now if you access the column "Total" of the table you will get the desired result.