Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagrid cell mouse click

Status
Not open for further replies.

jpinto

Technical User
Dec 12, 2003
75
PT
Hello,

I've a DataGridView on a form and I need to catch the mouse click event on a specific cell so that I can display more information about the value showned. For other words, if a user clicks on a certain cell of the datagrid, I want to open a new dialog form to show more information about the value displayed on the datagrid.

Thanks,

João Pinto
 
This code will help you somewhere...
Code:
    Private Sub d1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles d1.MouseUp
        Dim x As Integer = e.X
        Dim y As Integer = e.Y
        Dim pt As Point
        Dim ht As DataGridView.HitTestInfo
        ht = d1.HitTest(x, y)
        If e.Button = Windows.Forms.MouseButtons.Left Then
            'put your code here for any menu
        End If
        If ht.Type = DataGridViewHitTestType.Cell Then
            pt = d1.CurrentCellAddress()
            'you will get x, y in pt here.
            'pt(0 index based column, 0 index based row)
            'based on this you can provide your logic here.
        End If
    End Sub

Sharing the best from my side...

--Prashant--
 
OK, now I can validate if the user clicked on a specific column of the datagrid. What I need to know next is the value from the RecordID field regarding that particular cell that I've clicked. In other words, lets say I have a datagrid showing fields Name, Address, City and when I click on the cell regarding address I need to get the ClientID field from the database. How can I get that?

Thanks,

João Pinto
 
Use a CurrencyMAnager and a DataRowView.

'Declare these global to the form with the DataGrid
Dim cm As CurrencyManager
Dim drv As DataRowView


*********************

Next, where you bind the data to the datagrid:

DataGrid1.DataSource = DataSet1.Tables(0)

cm = DataGrid1.BindingContext(DataSet1.Tables(0))


Now, add to the MouseUp event

Private Sub d1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles d1.MouseUp
Dim x As Integer = e.X
Dim y As Integer = e.Y
Dim pt As Point
Dim ht As DataGridView.HitTestInfo
ht = d1.HitTest(x, y)
If e.Button = Windows.Forms.MouseButtons.Left Then
'put your code here for any menu
End If
If ht.Type = DataGridViewHitTestType.Cell Then
pt = d1.CurrentCellAddress()
'you will get x, y in pt here.
'pt(0 index based column, 0 index based row)
'based on this you can provide your logic here.

'the next line gets a DataRowView of the DataGrid row clicked
[red]drv = CType(cm.Current, DataRowView)[/red]
End If
End Sub

Then, to access the ClientID field, just use:

drv.Item("ClientID")

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
Where is e.X and e.Y defined?
 
It is one of the parameters of the d1_MouseUp sub:

Private Sub d1_MouseUp(ByVal sender As Object, ByVal [red]e As System.Windows.Forms.MouseEventArgs[/red]) Handles d1.MouseUp

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
I receive an error message on
Code:
drv = CType(cm.Current, DataRowView)

"Index -1 does not have a value."
 
well, this line:

If ht.Type = DataGridViewHitTestType.Cell Then

should allow execution of the 'drv = CType(cm.Current, DataRowView)' line only if an actual cell in the grid is clicked. If the header or blank space is clicked, this code should not execute. Since the 'drv = ' line should only execute when an actual cell is clicked, then you should not get the 'Index = -1" error. I use this construct extensively in my programming, and it works fine.

Perhaps you can put in a check that the DataGridView's CurrentRowIndex is greater than -1 before running the 'drv =' code.


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top