One row of list view is updating, but duplicate row is also inserted

102 Views Asked by At

Hello folks I am trying to update the quantity of a product that is being ordered. If the product already exists in the users 'basket' instead of inserting a new row, the quantity in the row with the existing item should be updated. It is updating but a new row is also inserted with the quantity that should have been added to the original row, like so:

enter image description here

I'm guessing something is wrong logically in my code but, I can't spot it.

 private void btn_add_Click(object sender, EventArgs e)
    {
        try
        {
            ListViewItem item = new ListViewItem(list_Select_Product.SelectedItems[0].Text);
            item.SubItems.Add(list_Select_Product.SelectedItems[0].SubItems[1].Text);
            item.SubItems.Add(txt_quantity.Text);

            bool ok = true;
            if (!validNumbers(txt_quantity))
                ok = false;
            if (!validLength(txt_quantity, 1, 2))
                ok = false;

            if (ok == true)
            {
                foreach (ListViewItem lvi in list_view_orderitems.Items)
                {

                    if(lvi.SubItems[0].Text == list_Select_Product.SelectedItems[0].Text)
                    {
                        int UpdateQunat = Convert.ToInt32(lvi.SubItems[2].Text);
                        int AddMe = Convert.ToInt32(txt_quantity.Text);
                        UpdateQunat = UpdateQunat + AddMe;
                        lvi.SubItems[2].Text = Convert.ToString(UpdateQunat);
                        list_view_orderitems.Items.Add(item);

                    }
                    else if (lvi.SubItems[0].Text != list_Select_Product.SelectedItems[0].Text)
                    {
                        list_view_orderitems.Items.Add(item);

                    }
                }
                if(list_view_orderitems.Items.Count == 0)
                {
                    list_view_orderitems.Items.Add(item);


                }

            }


        }
        catch
        {

            MessageBox.Show("A product must be selected");
        }
    }
2

There are 2 best solutions below

2
On BEST ANSWER

Look a little closer in the foreach loop. You actually add it there as well. (So you should remove that)

foreach (ListViewItem lvi in list_view_orderitems.Items)
{
    if(lvi.SubItems[0].Text == list_Select_Product.SelectedItems[0].Text)
    {
        int UpdateQunat = Convert.ToInt32(lvi.SubItems[2].Text);
        int AddMe = Convert.ToInt32(txt_quantity.Text);
        UpdateQunat = UpdateQunat + AddMe;
        lvi.SubItems[2].Text = Convert.ToString(UpdateQunat);
        // adding it again. This line is not needed.
        list_view_orderitems.Items.Add(item);
     }
     else if (lvi.SubItems[0].Text != list_Select_Product.SelectedItems[0].Text)
     {
         list_view_orderitems.Items.Add(item);
     }
}
0
On

I this that it this if you need to get rid of the item.Add (i marked it:

  if(lvi.SubItems[0].Text == list_Select_Product.SelectedItems[0].Text)
                    {
                        int UpdateQunat = Convert.ToInt32(lvi.SubItems[2].Text);
                        int AddMe = Convert.ToInt32(txt_quantity.Text);
                        UpdateQunat = UpdateQunat + AddMe;
                        lvi.SubItems[2].Text = Convert.ToString(UpdateQunat);
                      //  list_view_orderitems.Items.Add(item);

                    }