68

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

thevan
  • 10,052
  • 53
  • 137
  • 202

10 Answers10

147

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();
aloisdg
  • 22,270
  • 6
  • 85
  • 105
Jay Riggs
  • 53,046
  • 9
  • 139
  • 151
  • He don't want filteration expression, he just want to calculate all rows amount. In your answer, Filteration expression means, it will compute sum where EmpId = 5. – Muhammad Akhtar May 05 '11 at 05:31
  • It asks for filter. I dont have any conditions to filter. I want to sum all the rows in the datatable. – thevan May 05 '11 at 05:31
  • Jay Thanks for the solution. It reduces my number of line code. – John Aug 20 '13 at 11:01
39
 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();
aloisdg
  • 22,270
  • 6
  • 85
  • 105
Thomas
  • 63,911
  • 12
  • 95
  • 141
15

You can do like..

DataRow[] dr = dtbl.Select("SUM(Amount)");
txtTotalAmount.Text = Convert.ToString(dr[0]);
Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
8

If you have a ADO.Net DataTable you could do

int sum = 0;
foreach(DataRow dr in dataTable.Rows)
{
   sum += Convert.ToInt32(dr["Amount"]);
}

If you want to query the database table, you could use

Select Sum(Amount) From DataTable
FIre Panda
  • 6,537
  • 2
  • 25
  • 38
5
  public decimal Total()
    {
      decimal decTotal=(datagridview1.DataSource as DataTable).Compute("Sum(FieldName)","");
      return decTotal;
    }
Nikhil
  • 16,194
  • 20
  • 64
  • 81
Buntha Khin
  • 51
  • 1
  • 1
5

Compute Sum of Column in Datatable , Works 100%

lbl_TotaAmt.Text = MyDataTable.Compute("Sum(BalAmt)", "").ToString();

if you want to have any conditions, use it like this

   lbl_TotaAmt.Text = MyDataTable.Compute("Sum(BalAmt)", "srno=1 or srno in(1,2)").ToString();
Arun Prasad E S
  • 9,489
  • 8
  • 74
  • 87
2

You Can use Linq by Name Grouping

  var allEntries = from r in dt.AsEnumerable()
                            select r["Amount"];

using name space using System.Linq;

You can find the sample total,subtotal,grand total in datatable using c# at Myblog

0

Try this

int sum = 0;
foreach (DataRow dr in dt.Rows)
{
     dynamic value = dr[index].ToString();
     if (!string.IsNullOrEmpty(value))
     { 
         sum += Convert.ToInt32(value);
     }
}
NoWar
  • 36,338
  • 80
  • 323
  • 498
0

I think this solves

using System.Linq;


(datagridview1.DataSource as DataTable).AsEnumerable().Sum(c => c.Field<double>("valor"))
0

If you're wanting to do this within your cshtml file, you can write it like this (including a LAMBDA expression):

<td><b>£@Model.Sum(i => i.Amount)</b></td>

You can remove the html tags, I just left them in to try and help with the example.