Hi been working on this final piece of the puzzle and it seems like the last step is the hardest.

So far been able to stop it from adding multiple items per button click (number of rows + 1)
Update QTY count for first item only.

Trying to have it so all rows are checked and QTY updated, rather than adding new row per click for same item.

void UpdateProductList(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            if (DGV_cart.RowCount == 1)
            {
                string[] newRow = new string[] { button.Text.ToString(), button.Tag.ToString(), "1" };
                DGV_cart.Rows.Add(newRow);
                UpdateTotal();
                return;
            }
            else
            {
                Boolean found = false;
                foreach (DataGridViewRow row in DGV_cart.Rows)
                {
                    if (row.Cells[0].Value.ToString() == button.Text.ToString())
                    {
                        // row exists
                        found = true;
                        Console.WriteLine(button.Text + " already existed in DataGridView.");
                        // set QTY + 1
                        int newQTY = int.Parse(row.Cells[2].Value.ToString());
                        int plusOne = newQTY + 1;
                        row.Cells[2].Value = plusOne;
                        UpdateTotal();
                        break;
                    }
                    if (!found)
                    {
                        string[] newRow = new string[] { button.Text.ToString(), button.Tag.ToString(), "1" };
                        DGV_cart.Rows.Add(newRow);
                        UpdateTotal();
                        break;
                    }
                }
            }
        }

Recommended Answers

All 3 Replies

I think perhaps you need a little more explanation. Your code appears to do what you want and I don't see where the error you reference is being produced,

At time of orignal post, this was the issue below, every Button click just added as a new row, rather then complete the check to see if it already exists
list1.png

Just from moving thepublic Boolean found = false; to above the function has been able to kinda solve the issue above, but the first item added needs to have >1 quantity for other items added to increase
list_2.png

After a few clicks it just breaks anyways
error_1.png

So i'm thinking with the right tweaks and help is possible, the code will be a lot more robust and repeatable

Load the products and create a button if it has no barcode

private void LoadProducts()
        {
            int i = 1;
            string barcode = null;
            foreach (TabPage tp in TC_categories.TabPages)
            {
                MySqlConnection conn2 = new MySqlConnection(connStr);
                conn2.Open();
                string sql2 = "SELECT * FROM products WHERE prodCat = " + i.ToString();
                MySqlCommand cmd2 = new MySqlCommand(sql2, conn2);
                MySqlDataReader rdr2 = cmd2.ExecuteReader();

                FlowLayoutPanel flp = new FlowLayoutPanel();

                while (rdr2.Read())
                {
                    barcode = rdr2["prodBarcode"].ToString();
                    if (barcode != "")
                    {
                        // DO NOT create a button
                        // Console.WriteLine(rdr2["prodName"].ToString() + " == " + rdr2["prodBarcode"].ToString());
                    }
                    else
                    {
                        Button button = new Button
                        {
                            Size = new Size(100, 100),
                            Text = rdr2["prodName"].ToString(),
                            Tag = rdr2["prodPrice"].ToString()
                        };

                        button.Click += new EventHandler(UpdateProductList);

                        flp.Dock = DockStyle.Fill;
                        flp.Controls.Add(button);
                        flp.AutoScroll = true;
                        flp.AutoSize = true;
                        flp.AutoSizeMode = AutoSizeMode.GrowAndShrink;
                    }
                }
                conn2.Close();
                tp.Controls.Add(flp);
                i++;
            }

update datagridview on button click

public Boolean found = false;
        void UpdateProductList(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            int i = 0;
            if (DGV_cart.RowCount == 1)
            {
                string[] newRow = new string[] { button.Text.ToString(), button.Tag.ToString(), "1" };
                DGV_cart.Rows.Add(newRow);
                UpdateTotal();
                return;
            }
            else
            {
                foreach (DataGridViewRow row in DGV_cart.Rows)
                {
                    if (row.Cells[0].Value.ToString() == button.Text.ToString())
                    {
                        // row exists
                        found = true;
                        Console.WriteLine(button.Text + " already existed in DataGridView.");
                        // set QTY + 1
                        int newQTY = int.Parse(row.Cells[2].Value.ToString());
                        int plusOne = newQTY + 1;
                        row.Cells[2].Value = plusOne;
                        UpdateTotal();
                        break;
                    }
                    if (!found)
                    {
                        string[] newRow = new string[] { button.Text.ToString(), button.Tag.ToString(), "1" };
                        DGV_cart.Rows.Add(newRow);
                        UpdateTotal();
                        break;
                    }
                    i++;
                }
            }
        }
        void UpdateProductList(object sender, EventArgs e)
        {
            Button button = (Button)sender;

            bool Found = false;
            if (DGV_cart.Rows.Count > 0)
            {
                foreach (DataGridViewRow row in DGV_cart.Rows)
                {
                    if (Convert.ToString(row.Cells[0].Value) == button.Text)
                    {
                        // row exists
                        Console.WriteLine(button.Text + " already existed in DataGridView.");
                        // set QTY + 1
                        int newQTY = int.Parse(row.Cells[2].Value.ToString());
                        int plusOne = newQTY + 1;
                        row.Cells[2].Value = plusOne;
                        UpdateTotal();
                        Found = true;
                    }
                }
                if (!Found)
                {
                    string[] newRow = new string[] { button.Text.ToString(), button.Tag.ToString(), "1" };
                    DGV_cart.Rows.Add(newRow);
                    UpdateTotal();
                }
            }
            else
            {
                string[] newRow = new string[] { button.Text.ToString(), button.Tag.ToString(), "1" };
                DGV_cart.Rows.Add(newRow);
                UpdateTotal();
            }
        }

Few small tweaks and now it works perfectly

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.