views:

2057

answers:

4
A: 

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
+2  A: 

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"

Galwegian
+2  A: 

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];
Jason Jackson
I'm not using asp.net 3.5
mavera
Then put that in your question.
Jason Jackson
+2  A: 

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.
Binoj Antony