Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Change focus from DataGridView to Form while cell is in edit mode
 

Change focus from DataGridView to Form while cell is in edit mode

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
venir  Monday, August 03, 2009 10:45 PM
Thanks for the help, I got it figured out. I was already handling the CellEndEdit eventbut didn't have logic in placeto release the focus when there was no data entered. Got that added and it works as desired. Thanks again for the replies.
venir  Wednesday, August 05, 2009 5:58 PM
HI,
In datagridview there's event called cellendedit.you can try this event it may help you...
Best Regards, C.Gnanadurai
Gnanadurai  Wednesday, August 05, 2009 4:12 AM
Hi venir,

Could you please provide more information so that we can know why the button cannot get focus when you press it. I made a test project and handle the Click event of a button. The button can get focus when I press it. When you press the button, the DataGridView would lose focus and the CellEndEdit event would be fired. Then the DataGridView would accept the data and try to update it to the underlying data source. If there are some errors happened at this time,theDataGridViewwould fail to lost focus. The error can be that the cell value is illegal or cannot be null. The code snippet you provided does the same thing as handling the Click event of the button, so if the button Click event cannot be fired, the Form MouseClick event cannot be fired neither.

You need to handle some events to track and check the edited value to have the DataGridView can lose focus, such as CellEndEdit, CellLeave and so on.

Let me know if this helps.
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, August 05, 2009 11:50 AM
Thanks for the help, I got it figured out. I was already handling the CellEndEdit eventbut didn't have logic in placeto release the focus when there was no data entered. Got that added and it works as desired. Thanks again for the replies.
venir  Wednesday, August 05, 2009 5:58 PM

You can use google to search for other answers

Custom Search

More Threads

• Bound TextBox on form do not reflect changes made to TableAdapter
• Sorting Date Field in DataGridView Control
• DataBinding TextBox
• Selecting a row from a datagridview
• Which timer do I use and how?
• IE 8 addressbar type ComboBox
• Dynamic Property Grid Enum
• Two rows for headers !!
• How to add data & stored in a table via datagrid
• AddNew() creates a new row but I can't navigate to it