Tip Detecting user cell clicks in DataGridView objects

Rob Sherratt

Well-known member
Joined
Dec 30, 2012
Messages
64
Location
Corfu. Greece
Programming Experience
10+
Hope others will find this "Tip" useful (even though it's from a "newBie") ...

Double clicking a DataGridView object within Forms Designer generates the following Click event handler:

VB.NET:
Private Sub MyDataGridView_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles MyDataGridView.CellContentClick

End Sub

One "problem" with the default event handler is that it only responds to cell clicks if the cell in MyDataGridView is populated and the user happens to click EXACTLY on the text. There can be a problem with mouse alignment here!

IMHO, a better cell click event to use is this:

VB.NET:
Private Sub MyDataGridView_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles MyDataGridView.CellClick

End Sub

It has "better behaviour" for many user applications. Whenever the user clicks anywhere in the cell region the selection focus is changed and the CellClick event is generated immediately afterwards.

The cell co-ordinates in both cases are provided inside the Click event "e As DataGridViewCellEventArgs" instance as follows:

VB.NET:
MySelectedRow = e.RowIndex
MySelectedCol = e.ColumnIndex

Regards,
Rob
 
Last edited:
As the names suggest, one is for when the user clicks the content of a cell and one is for when the user clicks the cell. CellContentClick is most commonly used for Button columns, to detect when the user clicks a Button in a cell. It may also be used for a CheckBox column when you need to know the state of the CheckBox before CellValueChanged is raised.

Double-clicking on a control or component will only generate a handler for that class' default event. If you want to handle a different event then don't double-click the control or component and then change the declaration, especially if you just change the Handles clause and not the method name, which is very misleading. VB.NET provides two other mechanisms for generating event handlers so use one of those.

Firstly, you can use the drop-down lists at the top of the code window. Select the field from the left-hand list and then select the event from the right-hand list. That has been possible since the very first version of VB.NET.

Since VB 2005, you can also use the Properties window. Select the control or component in the designer first, open the Properties window and click the Events button, then double-click the appropriate event to generate a handler for it. This option has some added advantages in that it can generate a single method to handle multiple events if you select multiple items in the designer and you can also select an existing event handler from the drop-down instead of double-clicking to generate a new one.
 
@jmcilhinney@

Thanks again for your expert and most helpful replies. I am taking all your tips on board. Is there some more general tutorial or reference documentation or book on VS 2012 that I may read to more fully dispel my evident level of ignorance in using the IDE more effectively?

e.g. I wish with that in Visual Studio Express 2012, the Object Browser would give me meaningful information on the "Return Values". How do I make sense of the following return values "documentation"? Is there some way I can configure VS 2012 to display this data in a human readable way? e.g. the formatting and meaning of "Paramaters" is very clear.

InStr Help.jpg

If you want to handle a different event then don't double-click the control or component and then change the declaration, especially if you just change the Handles clause and not the method name, which is very misleading. VB.NET provides two other mechanisms for generating event handlers so use one of those.

Point very well made and taken on board, I edited my OP accordingly. Hope this is "correct" now.

Best regards,
Rob
 
The Object Browser appears to take its descriptions from the Help documentation but is not capable of reproducing all the formatting that may be found in the said documentation. That rather garbled bit of information in the Return Values section there is actually a table in the Help. If you encounter such a situation, simply select the method on the right-hand side of the Object Browser and hit F1 to open the corresponding Help documentation to see the information in all its formatted glory.
 
@hmcilhinney@

select the method on the right-hand side of the Object Browser and hit F1 to open the corresponding Help documentation to see the information in all its formatted glory.

Thank you so much for this tip - I didn't know about the F1 key doing this, but now I do, and it's just what I hoped for!

Thanks again,
Rob
 
Back
Top