I have a databound DataGridView and all columns are combobox. After the user selects a value from the combobox and focus is lost, I validate the dgv entry against other entries in the same column. If found to be invalid, I need to set focus back on the validated cell not the one that caused lost focus. I can successfully alter the value of the validated cell (to test cell reference only) on error but focus always remains on the cell that I clicked last. i.e. If I change the value in dgv(5,2) then click on dgv(6.2) and the validation of dgv(5,2) returns 'Invalid' the focus remains on dgv(6,2) but the value I set in dgv(5,2) is correct. Code below (set focus is lines 15-17):
Private Sub CellValueChanged(ByVal sender As Object, _ ByVal e As DataGridViewCellEventArgs) _ Handles grdRoster.CellValueChanged intGrdRow = grdRoster.CurrentCellAddress.Y intGrdCol = grdRoster.CurrentCellAddress.X If mblnFormActivated = True Then If grdRoster.CurrentCellAddress.Y > 0 And _ grdRoster.CurrentCellAddress.X > 4 Then CellValid = True CurrentName = grdRoster.CurrentCell.Value CheckPrevEntries() If CellValid = True Then Else grdRoster.CurrentCell = grdRoster(intGrdCol, intGrdRow) grdRoster.CurrentCell.Selected = False grdRoster(intGrdCol, intGrdRow).Selected = True End If End If End If End Sub '---------------------------------------------------------------------------------------------------! Private Sub CheckPrevEntries() '---------------------------------------------------------------------------------------------------! Dim NameOccurs As Short Dim EnteredStartTime As Date Dim EnteredEndTime As Date Dim RowStartTime As Date Dim RowEndTime As Date Dim TimeOverlap As Boolean TransPosition = "Start" TimeOverlap = False NameOccurs = 0 With grdRoster EnteredStartTime = TimeValue(CDate(grdRoster.Item(3, intGrdRow).Value)) EnteredEndTime = TimeValue(CDate(grdRoster.Item(4, intGrdRow).Value)) For indX = 0 To (.RowCount - 1) If indX <> intGrdRow Then 'Exclude current cell from comparison If grdRoster.Item(intGrdCol, indX).Value = CurrentName Then NameOccurs = NameOccurs + 1 RowStartTime = TimeValue(CDate(grdRoster.Item(3, indX).Value)) RowEndTime = TimeValue(CDate(grdRoster.Item(4, indX).Value)) If (EnteredStartTime >= RowStartTime And EnteredStartTime < RowEndTime) Or (EnteredEndTime > RowStartTime And EnteredEndTime < RowEndTime) Then TimeOverlap = True End If End If End If Next indX If TimeOverlap = False Then Else MessageBox.Show("This entry start/end times conflict with other entries for this person") CellValid = False Exit Sub End If If NameOccurs < 2 Then Else Action = MsgBox("Selected Name already has " & NameOccurs & " shifts for this date - Continue?", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "New Schedule Detail?") If Action = MsgBoxResult.No Then CellValid = False End If End If End With ChangesMade = True cmdSave.Enabled = True Exit Sub LocalError: MsgBox("frmRoster FaiLED - CheckPrevEntries: " & TransPosition & ":" & Err.Number & "-" & Err.Description & " ** PLEASE NOTE DETAILS OF THIS ERROR AND REPORT IT - ASAP **", MsgBoxStyle.Critical) If TransState = True Then mobjConn.RollbackTrans() TransState = False End If End Sub