Click here to Skip to main content
15,920,111 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have the strangest behavior happening with the DataGridView control in Visual Basic 2013 (Professional):

Scenario A (This code works exactly as expected - both events fire and I get two message boxes: "Click" and then "CellContentClick".):

Private Sub dgvPlanAssemblies_Click(sender As Object, e As EventArgs) Handles dgvPlanAssemblies.Click
    MsgBox("Click")
End Sub

Private Sub dgvPlanAssemblies_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvPlanAssemblies.CellContentClick
    MsgBox("CellContentClick")
End Sub


Scenario B (Nothing fires in this scenario - no message box):

Private Sub dgvPlanAssemblies_Click(sender As Object, e As EventArgs) Handles dgvPlanAssemblies.Click
    ''MsgBox("Click")
End Sub

Private Sub dgvPlanAssemblies_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvPlanAssemblies.CellContentClick
    MsgBox("CellContentClick")
End Sub


Has anyone else experienced anything like this? Why would one event depend on the other? By the way, the only code in the Click event that allows the other event to fire is a message box. I tried replacing the message box with other code--no event fires in that situation either.
Posted
Comments
Sergey Alexandrovich Kryukov 23-Mar-15 14:32pm    
You are showing only the methods which are supposed to be event handles for Click and CellContentClick events, but there is no evidence that they really are. The handlers are added to the invocation list of some event instance using the += operator — check it up. By the way, never use those auto-generated name; they come from the designer and violate (good) Microsoft naming conventions. This is what you have refactoring engine for.

Also, I see no difference between first and second code fragments. What scenarios? If you need help, you need to provide comprehensive code sample. Make it self-contained, simplify down for this purpose...

—SA
Kenny-A 23-Mar-15 15:08pm    
1. The "Handles dgvPlanAssemblies.Click" and "Handles dgvPlanAssemblies.CellContentClick" are what makes them event handlers.
2. The difference is that the "MsgBox("Click") line has been commented out.
Sergey Alexandrovich Kryukov 23-Mar-15 15:17pm    
All right, thank your for the explanation; I did not noticed.
Anyway, you can use the debugger. Put a breakpont on fist instructions of the handlers.
—SA
Kenny-A 23-Mar-15 16:06pm    
Thank you. I had originally used a breakpoint, which never was never triggered in "Scenario B". I figured it out, though. Another event was interfering with the CellContentClick event, preventing it from being fired. When the message box in the Click event fired, it prevented the other event from firing, which meant that the CellContentClick event could fire.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900