try to put your code in cell validating event
Private Sub DataGridView1_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
With DataGridView1.Rows(e.RowIndex)
If e.ColumnIndex = 0 Then
If Not ItemExists(.Cells(e.ColumnIndex).Value) Then
MessageBox.Show("Item Name Is not in the Items list", "Item Name Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
e.Cancel = True
End If
End If
End With
End Sub
Function ItemExists(ByVal itemname As String) As Boolean
Dim bResult As Boolean = True
connect()
Sql = "Select ProductName from Products where ProductName = '" & itemname & "' "
objcmd = New SqlCommand(Sql, objcon)
OBJDR = objcmd.ExecuteReader
If OBJDR.Read = False Then
bResult = False
End If
Return bResult
End Function