Tuesday, February 05, 2008

Calculate the sum of a column in a dataset

Problem : Calculate the sum of a column in a dataset

Solution :
After filling the dataset from your query you can calculate the sum of a numeric column.

Dataset's DataTable provides a default method called Compute through which you can perform any aggregate function based operation on columns in a DataTable.

Suppose you have

1. Column : Salary
2. DataSet : dsData
3. DataTable Name : Payroll

Then

dsData.Tables("Payroll").Compute("SUM(Salary)", String.Empty)

Gives the sum of that column.

Compute accepts two arguments : 1. Expression (Any aggregate Expression)
2. Filter (Any Filtering option, here String.Empty means no filter value)

4 comments:

Anonymous said...

how can i change the format of the data comlung into 2 decimal only?

Anonymous said...

i have two datasets, say firstDS, secondDS.

in both the datasets, have two columns,say ID,value.

need to sum the value depends on the same ID in the both datasets, how can do?

Anonymous said...

loop it based on ID and sum the values

Dorababu said...

How can I filter based on the column assume I am having my data-set as follow

Name Amount
ABC 20
ABC 20
DEF 20

Now I would like to get my output as follows


Name Amount
ABC 40
DEF 20