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 8355553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:53:54+00:00 2026-06-09T09:53:54+00:00

Basic concept of what I have is that there are two data grids. One

  • 0

Basic concept of what I have is that there are two data grids. One is populated with a material type and quantity I want as a base line. This table has unique material types. The second table may have multiple entries of the same type of material with differing quantities.

Table 1                  Table 2    
F01 20/40  150           F01 20/40     40
F01 30/50  150           F01 20/40     50
F01 50/90  150           F01 20/40     100
F01 70/110 150           F01 30/50     60

Since each material type in table 1 is unique, I’m taking each row (cell index [0]) and cycling through each row in the second column and adding up the quantity for each matching material type.

I’m stuck as I have been for a while why my loop keeps breaking after successfully tallying up the first material type. Here is my code:

private void combineButton_Click(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        DataRow dtrow;
        dt.Columns.Add("Material");
        dt.Columns.Add("OnHand_Qty");
        int x = 0;
        foreach (DataGridViewRow row in baseStockGrid.Rows)
        {
            string material = row.Cells[0].Value.ToString();
            foreach (DataGridViewRow crow in currentOnHandGrid.Rows)
            {
                string check = crow.Cells[0].Value.ToString();
                if (check == material)
                {
                    var y = crow.Cells[1].Value.ToString();
                    x = int.Parse(y) + x;
                }
                if (check != material)
                {
                    continue;
                }
            }
            dtrow = dt.NewRow();
            dtrow[0] = material.ToString();
            dtrow[1] = x;
            dt.Rows.Add(dtrow);
            x = 0;
            currentOnHandGrid.DataSource = dt;
        }
    }  

I basically refresh the data grid to show the combined material counts and it displays like this:

Table1                 Table2
F01 20/40   150        F01 20/40   190
F01 30/50   150        F01 30/50   0  //<---- Should show 60 according to example above.
  • 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-09T09:53:55+00:00Added an answer on June 9, 2026 at 9:53 am

    It appears that you are overwriting your own data source. At the beginning of the code, you declare a DataTable dt as a new table and set it up to store your refreshed data. After you pull the appropriate data for your first material type from currentOnHandGrid and store it in table dt, you overwrite currentOnHandGrid with dt. Now, you have a table that has your first material filled in with no other materials included. When you go to the next material in the outer loop, the inner loop tries to look for that material in the table, but it is not there as you have overwritten the original table with a fresh one.

    Simply move the part where you overwrite the old table with the new one to the outside of the loop and it should write properly.

    EDIT: Also, as the other answerer noted, you do not need the Continue statement. In addition, having an if-statement to check a condition and another if-statement to check the complement is bad form. The second if-statement should actually be an else-statement, as it is a boolean statement. “If it’s this, then do this, or else do that.” You don’t even need the else-statement with a continue inside it either. If the first if-statement fails, the program should continue anyway.

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

Sidebar

Related Questions

I have two basic interface-related concepts that I need to have a better understanding
For example, I have a class that has two strings, one of which must
Names and objects have been simplified for clarity's sake. The basic concept remains the
I have gone through Django's basic Poll tutorial, and I realize that I can
I have a basic Spring MVC controller that looks like this: @Controller public void
I have a simple concept question. I have a class that snaps a picture
I have an application that is very connection-based, i.e. multiple inputs/outputs. The UI concept
Although I know the basic concepts of binary representation, I have never really written
I am new to python decorators. I have understood the basic concepts with the
I'm new to Java EE and have been struggling with some basic middleware concepts

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.