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;
}
}
}
}