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)

3 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?

Kiran Kumar said...

loop it based on ID and sum the values

Copyright © 2008 - Protocols Of Matrix - is proudly powered by Blogger
Smashing Magazine - Design Disease - Blog and Web - Dilectio Blogger Template