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

后端 未结 9 1133
悲哀的现实
悲哀的现实 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:44

    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();
    
    0 讨论(0)
  • 2020-11-27 04:51

    You can do like..

    DataRow[] dr = dtbl.Select("SUM(Amount)");
    txtTotalAmount.Text = Convert.ToString(dr[0]);
    
    0 讨论(0)
  • 2020-11-27 04:51
      public decimal Total()
        {
          decimal decTotal=(datagridview1.DataSource as DataTable).Compute("Sum(FieldName)","");
          return decTotal;
        }
    
    0 讨论(0)
提交回复
热议问题