Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8320535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:40:54+00:00 2026-06-08T22:40:54+00:00

I wrote this formula here but for some reason it sums ALL the values

  • 0

I wrote this formula here but for some reason it sums ALL the values in the amt column instead of the one that has the itemcolumn string. Any ideas?

foreach (DataGridViewRow row in itemGrid.Rows)
{
    if (itemGrid.Rows[e.RowIndex].Cells["itemColumn"].Value.ToString() == itemcolumn)
        total += Convert.ToDecimal(row.Cells["Amt"].Value.ToString());                             
}

Here’s the full code so you can see what’s going on here.

            private void itemGrid_CellValueChanged(object sender, DataGridViewCellEventArgs e)
            {
                //
                // [This string compares amt and amtvalue on Cell edit]
                //
                if (itemGrid.CurrentCell.ColumnIndex == 5 && isCalled)
                {
                    // Prevents loop.
                    isCalled = false;

                    decimal total = 0;
                    string itemcolumn = String.Empty;

                    {
                        foreach (DataGridViewRow row in itemGrid.Rows)
                        {
                            foreach (DataGridViewCell cell in row.Cells)
                            {
                            if (cell.Selected)
                                {
                                    itemcolumn = Convert.ToString(row.Cells["itemColumn"].Value);

                                    string strName = vendorBox.Text;

                                    string Select = "select sum(case allocated when 'Received' then amt when 'Shipped' then -amt end) as amt from lineitem WHERE item='" + itemcolumn + "';";

                                    MySql.Data.MySqlClient.MySqlConnection conDatabase = new
                                    MySqlConnection(ConnectionString.connString);
                                    MySql.Data.MySqlClient.MySqlCommand cmdDatabase = new
                                    MySql.Data.MySqlClient.MySqlCommand(Select, conDatabase);

                                    try
                                    {
                                        conDatabase.Open();

                                        MySql.Data.MySqlClient.MySqlDataReader rdrRepairOrder;
                                        rdrRepairOrder = cmdDatabase.ExecuteReader();

                                        while (rdrRepairOrder.Read())
                                        {
                                            string rowz5 = string.Format("{0}", rdrRepairOrder.GetString(0));
                                            string wut5 = rowz5.ToString();
                                            itemamt = Convert.ToInt32(wut5);
                                        }

                                        rdrRepairOrder.Close();
                                        conDatabase.Close();
                                    }

                                    catch (MySql.Data.MySqlClient.MySqlException ex)
                                    {
                                        MessageBox.Show("Error " + ex.Number + " has occurred: " + ex.Message,
                                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    }
                                }
                            }
                        }
                    }

                        // Converts amt column.
                        // amt = The cell amount.
                        // itemamt = What's on the SQL table.

                        foreach (DataGridViewRow row in itemGrid.Rows)
                        {
                          if (itemGrid.Rows[e.RowIndex].Cells["itemColumn"].Value.ToString() == itemcolumn) total += Convert.ToDecimal(row.Cells["Amt"].Value.ToString());                             
                        }
                        MessageBox.Show("Total is: " + total.ToString());

                            int amt;
                            string amtz = itemGrid.Rows[e.RowIndex].Cells["Amt"].Value.ToString();
                            amt = Convert.ToInt32(amtz);


                            int value;
                            value = itemamt - amt;

                            if (value < 0)
                            {
                                MessageBox.Show("Cannot allocate more than what it is in inventory. You have " + itemamt.ToString() + " in stock.");
                                itemGrid[5, itemGrid.CurrentCell.RowIndex].Value = itemamt;
                            }

                        }

                        isCalled = true;
                    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T22:40:55+00:00Added an answer on June 8, 2026 at 10:40 pm

    Instead of itemGrid.Rows[e.RowIndex], try using row in your condition.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this code for zoom in/out . it works but even with one
I wrote this very simple macro to delete all rows when Column P has
I wrote this program: #include <stdio.h> /*Part B Write a program that: defines an
I wrote this class that draws a animated progress with a circle (it draws
How do I write the formula that would mean this: If X<$999.00 then multiply
I'm doing some math with really big numbers (I'm using Python, but this question
I have a feeling that this question is very simple, but I just can't
I wrote a program in C to generate fractals using a formula, but it
I'm trying to achieve this effect with jQuery. I wrote some of the code,
I wrote this as a simple dice game. It works as I want except

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.