Windows Develop Bookmark and Share   
 index > Windows Forms General > DataGridView -Need to increase number of events that fire
 

DataGridView -Need to increase number of events that fire

I have a DataGridView in a VB2005 program that works well except for one thing. When a user clicks a button outside the DGV, the CellValidate event fires, then the CellChanged event, and a bunch of other events, like RowLeave fire. The problem is that the event that is supposed to fire under the button never fires. It previously fired when we had fewer events that fired for the DGV.

How can I change the settings, number of events, or something so that the code under the button on another part of the form will fire when a user is in the DGV and then clicks the button on another part of the form.

Price Brattin  Monday, February 04, 2008 5:27 AM

Hi Price,

When the current cell in a DataGridView is in edit mode and you have typed something in this cell and then you click a button on this form, the DataGridView's events will occur in the following sequence:

CellLeave,

RowLeave,

CellValidating,

CellParsing,

CellValueChanged,

CellValidated

If you don't set the DataGridViewCellValidatingEventArgs.Cancel property to false in the CellValidating event handler, the focus will be moved from the current cell to the button and the button's Click event handler will be called.

Did you set the DataGridViewCellValidatingEventArgs.Cancel property to false in the CellValidating event handler in your application?

Linda

Linda Liu  Tuesday, February 05, 2008 8:06 AM

Hi Linda,

I am a little bit confused. I thought setting the cancel to true would negate the change in the cell and return focus to the cell. The value of cancelis false when the CellValidate code starts, right? Regardless, except for one column I don't set the cancel to anything in the cellvalidating event. For that one column, I set it to true if the value is not what it should be.

The action in theprogram is this: I have a "Save" button on the form that does a SQL transaction.Commit. After being in the datagridview, when the user clicks that button, the various events for the grid fire but the code for the click event for the button does not fire. The button has focus but the click event does not fire. It did before but it stopped working after I added a bunch more code in the CellValueChanged section That additional code in the CellValueChanged section calculates some values for some unbound columns.

I consulted a VB2005 guru from the Tulsa .Net Users Group and he suggested adding some code for the MouseHover event on the save button. That code is below. Now when the user moves the mouse pointer over the save button, the various events for the DGV fire. And the code under the click event for the Save button fires when the user clicks it.

What do you think? Is there a reason the click event for the Save button will not fire after moving the mouse from the grid to the button and clicking on it? That is, it will not fire unless I have the MouseHover code as below.

Private Sub btnSave_MouseHover(ByVal sender As System.Object, ByVal e As System.EventArgs) _

Handles btnSave.MouseHover

btnSave.Focus()

End Sub

Price Brattin  Tuesday, February 05, 2008 3:35 PM

Hi Price,

> I am a little bit confused. I thought setting the cancel to true would negate the change in the cell and return focus to the cell.

Sorry that I miswrote in my previous reply. It should be "setting the DataGridViewCellValidatingEventArgs.Cancel property to true will prevent the focus from leaving the current cell".

> Regardless, except for one column I don't set the cancel to anything in the cellvalidating event. For that one column, I set it to true if the value is not what it should be.

Does the problem exist when you don't set the cancel to true from within the CellValidating event handler?

Since I couldn't reproduce the problem on my side, it would be better if you could create a simple example that could just reproduce the problem and send it to me. To get my actual email address, remove 'online' from my displayed email address.

Linda

Linda Liu  Thursday, February 14, 2008 10:27 AM

You can use google to search for other answers

Custom Search

More Threads

• OnMessageBox event in WebBrowser control? (alert/error/notify etc)
• Checkboxes in ListView
• Add TrackBar in StatusStrip
• Paging in DatagridView in C# Windows Application with Binding Source
• CBT Hook not working in Windows Vista
• drag and drop images from tree view nodes
• HOW TO: set icon to winform application and work with browser.
• how do i know which portion of a ToolStripSplitButton is enterd?
• testing modules of my application
• Develop an "Item" instead of just a control