Skip to main content

datatable compute sum in c# | datatable compute() function

datatable compute sum in c# | datatable compute() function

Hi Readers, Today I am going to explain you the use of compute() function of Data table using C#.NET

Let's begin..

datatable compute sum in c# ,calculate sum of datatable column c# ,   datatable column sum c#,datatable compute,c# datatable column average,find minimum value in datatable,find max value in datatable column c#
datatable column sum in c#

datatable compute sum in c# | datatable compute() function


Let's say that we have the following rows filled in the Data table with the columns ID,NAME,AGE and SALARY as shown in snapshot below:-

datatable compute sum in c# ,calculate sum of datatable column c# ,   datatable column sum c#,datatable compute,c# datatable column average,find minimum value in datatable,find max value in datatable column c#
employees detail in datatable



In order to calculate the figures like total of salary, average of salary, maximum salary and minimum salary, we can use the compute() function of the Data table.

The compute() function loops through each value of the column's cell and then perform the calculation.

For the above data table's salary column, lets find the total, average, maximum and minimum of salary one by one using compute() function:-

datatable compute sum in c# | datatable compute() function

  1. Calculate the total of salary:  
To calculate the total of the salary, we need to have the following lines of code in C#.NET:

decimal salaryTotal=0;
salaryTotal = Convert.ToDecimal(dtEmployee.Compute("SUM(SALARY)", string.Empty));
dtEmployee.Rows.Add(DBNull.Value,"TOTAL:-",DBNull.Value,total);

The above code on execution will calculate the total and add the calculated total in the last row as shown in snapshot below:

datatable compute sum in c# ,calculate sum of datatable column c# ,   datatable column sum c#,datatable compute,c# datatable column average,find minimum value in datatable,find max value in datatable column c#
employees detail with total salary
As shown in the above snapshot, the last row is added using Add() function of Data table showing the total of salary column.


The compute() function takes 2 arguments, one to calculate the total of the salary column as "sum(salary)" and second as "string.Empty" which filters the value of the salary column i.e. it will avoid the cell having no value in it. The value may be an empty string or null value.

         2. Calculate the average of salary:

To calculate the average of the salary, we need to have the following lines of code in C#.NET:

decimal averageSalary=0;
averageSalary=Convert.ToDecimal(dtEmployee.Compute("avg(SALARY)",string.Empty));
dtEmployee.Rows.Add(DBNull.Value,"Salary Average:-",DBNull.Value, averageSalary);

The above code on execution will calculate the average and add the calculated average in the last row as shown in snapshot below:

datatable compute sum in c# ,calculate sum of datatable column c# ,   datatable column sum c#,datatable compute,c# datatable column average,find minimum value in datatable,find max value in datatable column c#
employees detail with average of salary

The compute() function takes 2 arguments to find the average of salary. 
The first argument "avg(salary)" find the average of salary among the different values of the salary column.             
The second argument is passed as "String.Empty" which will avoid calculation of the cell having value as empty string or null value as cell having such value will generate run time error during execution.

       3. Find the maximum/highest salary:

To find the max of the salary column of the Datatable , we need to have the following lines of code in C#.NET:

decimal maxSalary=0;
maxSalary=Convert.ToDecimal(dtEmployee.Compute("max(SALARY)", string.Empty));
dtEmployee.Rows.Add(DBNull.Value,"Max Salary:-", DBNull.Value, maxSalary);

The above code on execution will find the maximum or highest salary and add it to  the last row as shown in snapshot below:

datatable compute sum in c# ,calculate sum of datatable column c# ,   datatable column sum c#,datatable compute,c# datatable column average,find minimum value in datatable,find max value in datatable column c#
employees detail with the maximum salary in the last row

The compute() method takes 2 arguments to find the maximum of salary. 
The first argument "max(salary)" find the maximum or highest salary among the different values of the salary column.      
The second argument is optional and  can be passed as "" but it will give error message when there will be an empty string or null value in the salary column. 
So to avoid such value, we need to pass the second argument as "String.Empty" so that such value can be avoided.

          4. Find the minimum/lowest salary:

To find the minimum of the salary column of the above Data table , we need to have the following lines of code in C#.NET:

decimal minSalary=0;
minSalary=Convert.ToDecimal(dtEmployee.Compute("min(SALARY)",string.Empty));
dtEmployee.Rows.Add(DBNull.Value,"Minimum Salary:-",DBNull.Value, minSalary);

The above code on execution will find the minimum or lowest salary and add it to  the last row as shown in snapshot below:

datatable compute sum in c# ,calculate sum of datatable column c# ,   datatable column sum c#,datatable compute,c# datatable column average,find minimum value in datatable,find max value in datatable column c#
employees detail with the minimum salary in the last row
The above code works the same way as that of the maximum of the salary code. 
The only difference is that we need to pass the first argument as "min(salary)". 
That's it and we will get the lowest salary among all the rows.

Therefore if we summaries the use of the compute() function, we can conclude that -
    "compute() function is used to find the":-
  1. Total.
  2. Average.
  3. Maximum.
  4. Minimum.
That's it!!!

Hope you have enjoyed by programming the above code in your project. 
Please comment if you have any query regarding the above coding. 
And share with your co-programmers to help them out in coding. 
  
Enjoy Coding!!!!

LIKE COMMENT & SHARE

Comments

Popular posts from this blog

Adding rows to Datatable using C#.NET

Adding rows to Datatable using C#.NET Hi Readers, Today I am going to explain you 2 ways that we can use to add rows to a Data table using C#.NET.  So lets begin: Add row in datatable in c# Adding rows to Datatable using C#.NET > Lets say, we have a data table with the columns ID, NAME, AGE AND SALARY and I want to add rows to this data table using C# coding . > First of all, add columns to data table using the following lines of code: DataTable dtEmployee = new DataTable("Employee"); dtEmployee.Columns.Add("ID", typeof(int)); dtEmployee.Columns.Add("NAME", typeof(String)); dtEmployee.Columns.Add("AGE", typeof(int)); dtEmployee.Columns.Add("SALARY", typeof(Decimal)); Add row in datatable in c# > Now if we want to add rows to the above data table at run time, we have the following 2 ways to do so: Adding rows to Datatable using C#.NET 1. Using DataRows :- In this, we create an o

Project available: IPMS

Following project is ready available for academic purpose and business requirements: In-Patient Management System (IPMS): Objective: This project fulfills the requirements of a clinic or hospital to manage in-patient activities i.e. patients who are admitted to the hospital. The objective of “ In-Patient Management ” is to provide such a system that can make the in-patient process automated.  The system will help the users for the following operations:- Ø   The patients’ detail, their medical history information, tests reports that have been done during their stay in the hospital. Ø    The system will even help the users to keep details for medical prescriptions given to the patients. Ø   The system will be able to keep the staff detail (doctors and nurses) detail, their schedule of work etc. Ø   The system will help the nursing staff to allot bed to the patient after having look on the current booking status of  bed i.e. whether it is being booked for o