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


hello folks trying update quantity of product being ordered. if product exists in users 'basket' instead of inserting new row, quantity in row existing item should updated. updating new row inserted quantity should have been added original row, so:

enter image description here

i'm guessing wrong logically in code but, 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 selected");         }     } 

look little closer in foreach loop. add there well. (so 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 again. line 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);      } } 

Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -