Confirm DataGridView by clicking a menu item

Hi I have a window form containing a menu bar, a toolbar, and a DataGridView control. I am using VB.Net.

I have a save button on the toolbar and a save menu item. I need the check to be done before saving. My problem is that if the cell value changes and then any of the save buttons is pressed, the check is not performed and therefore the new value is not saved.

I looked at events in a DataGridView, such as a leave event, however no one seems to be doing what I want. That is, “leave” does not start when you press any of the save buttons.

Does anyone know how I can run my verification code when I click one of these buttons (or any other button).

Any help is much appreciated!

+7
source share
4 answers

The problem is that clicking on a button in a ToolStrip control or a MenuStrip control MenuStrip not cause the Validating / Validate events to be raised by the currently selected control. This is by design. Interaction with menus and toolbars does not cause the currently selected control to lose focus, which means that validation never works.

If you think about it carefully, it even makes sense. Imagine that your validation logic required that each cell contain a value (i.e., it did not allow the cells to remain empty or empty). If a verification event was triggered every time a user tried to select an item from a menu or toolbar, they couldn’t even “insert” anything into a cell!

You will have to add a bunch of ugly, extra logic to your form if you insist on overriding this behavior and you run the risk of seriously annoying your user. Suffice it to say that I do not recommend it.

The best solution is to extract your validation logic into a separate method and manually call it at the beginning of the event handler method for each menu item / toolbar that you want to trigger. Alternatively, you can raise the LostFocus event for your DataGridView control at the beginning of your Save button event handler control using the InvokeLostFocus method. Something like:

 InvokeLostFocus(myDataGridView, EventArgs.Empty) 
+1
source

For me, the call to the Validation function in my form from the context menu event was sufficient to do what I needed, which included forcing grid validation events.

+1
source

While validation is performed on controls such as the TextBox immediately after the Leave event, the DataGridView control only validates in edit mode and only for each cell.

You can handle the CellValidating event on the DataGridView to capture the moment when the current cell passes the test - this event will be fired if the focus moves from the grid to your save button. If the cell value is not valid, you can set the Cancel property of the DataGridViewCellValidatingEventArgs object to true .

0
source

I had the same problem and none of the answers in different forums did this for me. In my case, I had several grids in shape. And even some lattices in other UserControls. Therefore, I created the following routines and called CommitGridEdit () in the corresponding ToolStrip click events. I think this is a trick.

 Public Sub CommitGridEdit(ByVal frm As Form) ' Clicking on a ToolStrip menu item will not cause the grid to end editting the current cell. ' So we have to find if any grid is in edit mode and do this ourself. Dim grid As DataGridView = GetFocusedGrid(frm) If grid Is Nothing Then Exit Sub If Not grid.CurrentCell Is Nothing Then If grid.CurrentCell.IsInEditMode Then grid.EndEdit() End If End If End Sub Private Function GetFocusedGrid(ByVal ctl As Control) As DataGridView For Each c As Control In ctl.Controls If TypeOf c Is DataGridView AndAlso c.ContainsFocus Then Return c ElseIf c.Controls.Count > 0 Then Dim c2 As Control = GetFocusedGrid(c) If Not c2 Is Nothing Then Return c2 End If Next c Return Nothing End Function 
0
source

All Articles