Remove duplicated row from datagridview c# -
i want unique rows. loop should check rows in datagridview , see if matches text textbox, if not, add value textbox datagridview. problem: first row works, loop iterate through first row in datagridview, why wont continue through other rows?
private void laggtill_click(object sender, eventargs e) { bool dublett = false; foreach (datagridviewrow r in datagridview1.rows) { if(r.cells[0].value.tostring() == txtnamn.text) // txtnamn textbox { dublett = true; messagebox.show("varan finns redan, gör om!"); } break; } if(dublett == false) { datarow dr; dr = dt.newrow(); dr["namn"] = txtnamn.text; dr["pris"] = txtpris.text; dr["varunummer"] = txtvnr.text; dr["saldo"] = txtsaldo.text; dt.rows.add(dr); datagridview1.datasource = dt.defaultview; clearrow(); spara(); } txtnamn.text = ""; txtpris.text = ""; txtsaldo.text = ""; txtvnr.text = ""; }
if(r.cells[0].value.tostring() == txtnamn.text) // txtnamn textbox { dublett = true; messagebox.show("varan finns redan, gör om!"); break; }
move break; inside if.
Comments
Post a Comment