How to Calculate the Sum of the Datatable Column in ASP.NET

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

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();

Get SUM of DataTable column

You can use DataTable.Compute method

var sumOfA = dt.Compute("Sum(Call)", "[Affiliate Name] = 'A'");

Or use LINQ

var sumOfA = dt.AsEnumerable()
.Where(dr => dr.Field<string>("Affiliate Name").Equals("A"))
.Sum(dr => dr.Field<int>("Call"));

In case you want get sum of calls for all values of Affiliate Name use LINQ with anonymous type

var results = dt.AsEnumerable()
.GroupBy(dr => dr.Field<string>("Affiliate Name"))
.Select((group, dr) => new
{
AffiliateName = group.Key,
CallsCount = group.Sum(dr.Field<int>("Call")))
});

foreach(var result in results)
{
// Use results
Console.WriteLine($"{result.AffiliateName}: {result.CallsCount}");
}

how to calculate sum (Total) of DataTable Columns using C#

Instead of dt.Compute you can also use LINQ:

int sum = dt.AsEnumerable().Sum(r => r.Field<int>("Salary"));

The nice thing with LINQ is that it's so easy to calculate the everage instead:

double average = dt.AsEnumerable().Average(r => r.Field<int>("Salary"));

or to get max salary:

int maxSalary = dt.AsEnumerable().Max(r => r.Field<int>("Salary"));

or to filter, count all salaries which are higher than 3000:

int countHighSalary = dt.AsEnumerable().Count(r => r.Field<int>("Salary") >= 3000);

How to sum columns in a dataTable?

Try this:

            DataTable dt = new DataTable();
int sum = 0;
foreach (DataRow dr in dt.Rows)
{
foreach (DataColumn dc in dt.Columns)
{
sum += (int)dr[dc];
}
}

What is the most efficient method to calculate total numbers in a DataTable Column

You'll need to loop over each row, that's for sure. However, you can get the column index once to avoid unnecessary looping for the columns:

int sum = 0;
DataSet ds = new DataSet();

DataTable dt = ds.Tables[3];
int columnIndex = dt.Columns["x column"].Ordinal;
foreach (DataRow dr in dt.Rows)
{
sum += Convert.ToInt32(dr[columnIndex]);
}

calculating the sum total of the columns and display in footer when colum names are dynamic

You can loop all the columns and rows in the DataTable and show the results in the footer.

DataTable dt = new DAL_Reports().Rpt_Count_Transpose(from_date, to_date);

//create a array to store the total column values
int[] RowTotals = new int[dt.Columns.Count];

//loop all the rows in the datatable
foreach (DataRow row in dt.Rows)
{
//loop all the columns in the row
for (int i = 0; i < dt.Columns.Count; i++)
{
//add the values for each cell to the total
RowTotals[i] += Convert.ToInt32(row[dt.Columns[i].ColumnName]);
}
}

//loop all the columns again to set the values in the footer
for (int i = 0; i < dt.Columns.Count; i++)
{
GridView1.FooterRow.Cells[i].Text = string.Format("{0:N2}", RowTotals[i]);
}


Related Topics



Leave a reply



Submit