[RESOLVED] [2008] DataGridView focus problem
Hi,
I've got a DataGridView which isn't receiving the focus.
I've got a bound DataGridView, and bound TextBoxes, so when the user scrolls through the grid, the text boxes display data associated with whatever is selected in the grid.
The user can press "U" (to update) the data, and a TabControl containing the text boxes becomes enabled so the user can make some changes.
What I'm finding is that if the user abandons the changes (where all the TextBoxes, Combo's revert back to their original state), I can't put the focus back to the DataGridView UNLESS I put a messagebox in (perhaps saying "Changes abandoned" - which might be ok, anyway).
If I don't put the message box in, the focus moves to the menus.
Its as if delaying the DataGridView.Focus, via the messagebox, allows the .Focus to work.
Re: [2008] DataGridView focus problem
:wave:
Show us the code surrounding the .Focus method.
Re: [2008] DataGridView focus problem
This works, but if I comment out the MessageBox, the focus isn't set to dgv.
Code:
Private Sub TextBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ....
If e.KeyCode = Keys.F10 Then
bind.CancelEdit()
Me.TabControl1.Enabled = False
Me.dgv.Enabled = True
Me.dgv.Focus()
MessageBox.Show("Changes have been abandoned", "Abort", MessageBoxButtons.OK, MessageBoxIcon.Information)
Re: [2008] DataGridView focus problem
:confused:
Have you tried commenting out the msgbox line and stepping through the code after the call to dgv.focus? I'm just curious as to whether it's hitting some other event code somewhere else in the app.
Re: [2008] DataGridView focus problem
:eek:
I didn't try that, but I'm glad I did now!
REALLY bizarre. I break on .Focus, and stepping through shows that it does everything that it should through the end of the sub. If I continue as normal when the sub has been processed, I go back to the form, and the dgv has the focus!!!
Re: [2008] DataGridView focus problem
I wonder if an application.doevents in the place of the msgbox will help?
Re: [2008] DataGridView focus problem
Errmmm, yes, this works. :ehh:
Now that was strange!
Many thanks for the suggestion!
Re: [2008] DataGridView focus problem
No prob. And I agree fully... it's wierd. I suspect this isn't the 'best' solution, but hey, it works. :D