How to calculate the sum of the datatable column in asp.net?

后端 未结 9 1173
悲哀的现实
悲哀的现实 2020-11-27 04:15

I have a DataTable which has 5 columns:

  • ID
  • Name
  • Account Number
  • Branch
  • Amount

The DataTable contains 5 rows.

9条回答
  •  情歌与酒
    2020-11-27 04:35

    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
    

提交回复
热议问题