vb.net - i want datagridview cell to be cleared and remain focused -
i have datagridview contains 4 columns item,qty,price,amount. user should enter item name in first column. have table in database item names stored. inserted name should checked if in database or not. if not in database want cell cleared textbox , focus remains in cell. user should not allowed continue until enters correct item name... far have tried this
private sub datagridview1_cellendedit(byval sender object, byval e system.windows.forms.datagridviewcelleventargs) handles datagridview1.cellendedit connect() sql = "select productname products productname = '" & me.datagridview1.rows(e.rowindex).cells(0).value.tostring & "' " objcmd = new sqlcommand(sql, objcon) objdr = objcmd.executereader if objdr.read = false messagebox.show("item name not in items list", "item name error", messageboxbuttons.ok, messageboxicon.error) ''clearing cell ' me.datagridview1.rows(e.rowindex).cells(0).value = "" '' here want cell foucus , cursor remain textbox datagridview1.currentcell = datagridview1.rows(datagridview1.currentcell.rowindex).cells(0) ' datagridview1.beginedit(true) exit sub end if end sub
here image of datagridview
this datagridview displayed user types can pick item names it.it works combobox autocomplete.
finally got looking for. put code in cell validating event
private sub datagridview1_cellvalidating(sender object, e datagridviewcellvalidatingeventargs) handles datagridview1.cellvalidating datagridview1.rows(e.rowindex) if e.columnindex = 0 if not itemexists(.cells(e.columnindex).value) messagebox.show("item name not in items list", "item name error", messageboxbuttons.ok, messageboxicon.error) e.cancel = true end if end if end end sub
function itemexists(byval itemname string) boolean dim bresult boolean = true connect() sql = "select productname products productname = '" & itemname & "' " objcmd = new sqlcommand(sql, objcon) objdr = objcmd.executereader if objdr.read = false bresult = false end if return bresult end function
Comments
Post a Comment