thevan thevan - 1 year ago 137
ASP.NET (C#) Question

How to calculate the sum of the datatable column in

I have a DataTable which has 5 columns:

  • ID

  • Name

  • Account Number

  • Branch

  • Amount

The DataTable contains 5 rows.

How can I show the sum of the Amount Column in a Label Control as "Total Amount"?

Answer Source

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;
table = dataSet.Tables["YourTableName"];

// Declare an object variable.
object sumObject;
sumObject = table.Compute("Sum(Amount)", "");

Display the result in your Total Amount Label like so:

lblTotalAmount.Text = sumObject.ToString();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download