We help IT Professionals succeed at work.

How to calculate the sum of all rows at each new row in C#

SniperCode Sheva
Last Modified: 2017-05-06
My app is a sort of cash register for a shop so in the datagrid there is Reference ,
it is the reference of the product.
When the user types the reference directly the information about the product will be filled in the other columns and the user can choose the quantity he wants and it will do the calculation.

ALSO, I tried to put the total of the bill in the datagrid...

Reference | Product |Quantity |Price |Total |Total Bill
AB              |AB           |2               |1000 |2000 |5000
BC              |BC            |3              |1000 |3000

This is what I am trying to do... To do it, I tried this code:

private void TAB_Credit_RowValidated(object sender, DataGridViewCellEventArgs e)
    int sum = 0;
    for (int i = 0; i < TAB_Credit.Rows.Count; ++i)
        sum += Convert.ToInt32(TAB_Credit.Rows[i].Cells["Total"].Value);
    TAB_Credit.Rows[e.RowIndex].Cells["Total_Bill"].Value = sum.ToString();

Open in new window

Watch Question

Dmitry GSenior Developer

I'd say it's not really correct, from an object perspective, to add "grand total" to a row - this is characteristic of a rows collection.

And if we talk about a "cash register", check any receipt from a cash counter (or invoice, or...) - the "grand total" is never printed in items rows. I can't see any point to change commonly accepted behavior.
Vishal TankariyaAsp.Net Developer

You can use compute function instead of for loop.

TAB_Credit.Rows[e.RowIndex].Cells["Total_Bill"].Value = TAB_Credit.Compute("sum(Total)", "id>0");

Open in new window

Dmitry GSenior Developer

Hmmmm... TAB_Credit is a DataGridView, as I understand. And dataGridView has NO "Compute" method, this method is on the DataTable class. Am I wrong? Or the above code is wrong?
Senior Developer
Unlock this solution and get a sample of our free trial.
(No credit card required)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.