How does one trap 'click' or 'double click' events in a datagrid cell?

HDokes

Active member
Joined
Jun 28, 2003
Messages
32
Hello,

I am using a datagrid on a database form in VB from which I would like to be able to establish and perform a piece of code upon a double click within the cell.

My dilema is that while in the edit mode of the form, when I double click on a any cell in the datagrid, the entire datagrid is selected and takes me to the following:

Private Sub grdButton_Assignment_Table_Navigate(ByVal sender As System.Object, ByVal ne As System.Windows.Forms.NavigateEventArgs) Handles grdButton_Assignment_Table.Navigate

End Sub

I can find no reference in the MSDN libraries that would show how one could trap on a double click within a cell and add code to that trap.

If I can not trap on a double click then I could otherwise allow the user to select a file through a button trap (which I have tried) however I can find no method for pushing the subsequent string of data into the last selected cell in the datagrid.

Any ideas?
 
The thing about Click and DoubleClick events is that they are at the Datagrid level and not the Cell level.
You can use the CurrentCellChanged event to pinpoint which Cell the user clicks on.
 
Or, with a lot of hassle, you could use the MouseDown event of the DataGrid, because the mousedown eventargs contain the X,Y coordinates.

This would make it possible for you to compute, which cell the cursor is actually over right now.
 
Heiko, why go to all that hassle when you can simply get the exact cell clicked using CurrentCellChanged. Either way were not getting the Double-click.

Code:
This gets the column and row...

Datagrid1.CurrentCell.ColumnNumber
Datagrid1.CurrentCell.RowNumber

or if you wanted to know the column name so as to perform a query based on the Dataset...

Dataset1.Tables(0).Columns(Datagrid1.CurrentCell.ColumnNumber).ColumnName.ToString
 
Greetings again,

Thanks Robby for the info. This at least allows me to push a text string into the last cell selected which in the end is what I wanted to achieve. Seems a bit odd however that there are no immediate mouse events that can be captured when clicking inside the datagrid. This would have made the process I am working on more seemless and would have eliminated a couple a step that the user is forced to initiate... i.e.... click on a button after selecting the cell.

I used the following line to fill the cell....

grdButton_Assignment_Table.Item(grdButton_Assignment_Table.CurrentCell.ColumnNumber, grdButton_Assignment_Table.CurrentCell.RowNumber) = openFileDialog1.FileName
 
Back
Top