I have a DataTable which has 5 columns:
The DataTable contains 5 rows.
How can I show the sum of the Amount Column in a Label Control as "Total Amount"?
sum() Sum the values in a data set. Fairly simply, this plug-in will take the data from an API result set and sum it, returning the summed value. The data can come from any data source, including column data, cells or rows.
To calculate the sum of a column in a DataTable use the DataTable.Compute method.
Example of usage from the linked MSDN article:
DataTable table = dataSet.Tables["YourTableName"]; // Declare an object variable. object sumObject; sumObject = table.Compute("Sum(Amount)", string.Empty);
Display the result in your Total Amount Label like so:
lblTotalAmount.Text = sumObject.ToString();
this.LabelControl.Text = datatable.AsEnumerable() .Sum(x => x.Field<int>("Amount")) .ToString();
If you want to filter the results:
this.LabelControl.Text = datatable.AsEnumerable() .Where(y => y.Field<string>("SomeCol") != "foo") .Sum(x => x.Field<int>("MyColumn") ) .ToString();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With