I have a a form with a DataGridView that is being used for data entry as a sales order. I have two buttons for printing and saving the order on the form that cannot be clicked if the DataGridView has a cell in edit mode. What I am trying to do is get it so that even if a cell is in edit mode on the DataGridView, the user can click on the buttons on the form and the DataGridView will commit the current edit and transfer focus to the form so that the buttons can be clicked. I have code that determines if a MouseClick event happens outside the DataGridView, but I don't know how to get the DataGridView to release the focus so the buttons can be pressed or even if this is the right direction to be heading in. Any help is appreciated.
Private Sub Form1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
If System.Windows.Forms.Cursor.Position.X > (DataGridView1.Location.X + DataGridView1.Size.Width) Or _
System.Windows.Forms.Cursor.Position.X < DataGridView1.Location.X Or _
System.Windows.Forms.Cursor.Position.Y > (DataGridView1.Location.Y + DataGridView1.Size.Height) Or _
System.Windows.Forms.Cursor.Position.Y < DataGridView1.Location.Y Then
DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
Me.Focus()
End If
End Sub