Click here to Skip to main content
16,022,924 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a datagridview control, with SelectionMode=FullRowSelect. This control also allows any column heading to be clicked to sort any column. The MouseClick event fires on this control when a column heading is clicked, which results in my code acting like the last row that was clicked (where 'focus' was) is clicked again, in addition to the datagridview sorting. I want to distinguish between a MouseClick that happens within the grid, and a MouseClick that happens when a column heading is clicked. Can't figure it out. I did add a ColumnHeaderMouseClick event, which does fire in addition to the MouseClick event when a column heading is clicked, but it fires after the MouseClick event (if it would fire before, then I'd have a workaround here). Any suggestions on how to determine if a MouseClick was on a column heading, or how to get the ColumnHeaderMouseClick to fire first? Any help is greatly appreciated. Thanks.
Posted

You can use the CellMouseClick[^] event or CellClick[^] event, either of which you could use to signal when a cell is clicked. I wouldn't use the MouseClick event anyway because if the user double clicks, the MouseDoubleClick event is fired instead.
 
Share this answer
 
What I found was that if I used the cellclick event, that allowed me to check e.RowIndex, which is a value not available in the mouseclick event. Then I can check if it = -1, which identifies a column heading. Problem solved - thanks.
 
Share this answer
 
Comments
Member 10964275 22-Mar-17 14:58pm    
So clear and smart!

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