NotQuiteThereYet
NotQuiteThereYet

Reputation: 497

Getting the X/Y coordinates of the "current cell" in a DataGridView?

[using VB 2010 / Winforms]

I have a DataGridView with several columns. It's unbound, and not connected to any sort of database or anything -- I am simply populating it cell by cell based on user input.

So anyways, one of the columns in the DGV is of type "image" (DataGridViewImageColumn).

What I'm trying to do is whenever one of the image cells is clicked, a context menu strip is shown at the exact loaction where the image cell is clicked.

Here's what I've got so far...

Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick

        Dim columnName As String = DataGridView1.Columns(e.ColumnIndex).Name
        If columnName = "Image" Then 
         Me.Status_ContextMenuStrip1.Show(Me.DataGridView1.CurrentCell.ContentBounds.Location) ' <-- This isn't right, but I must be close!
        End If

End Sub

When I run the above code and click on an image cell, the context menu appears, but it appears at the very top left corner of the screen. How can I get it to appear at the exact location that the clicked cell is at? I'd actually like it to appear just below the clicked cell, so that it has a similar visual effect to a combobox "drop down" (and I know how to offset the X and Y coordinates as soon as I can figure out how to get it in the general vicinity of where it needs to be).

Thanks!

Upvotes: 2

Views: 30623

Answers (3)

LuckyLuke82
LuckyLuke82

Reputation: 604

For anyone struggling with this in future - this is what really works:

'For forms
Dim f as New Form2
f.Location = DGV.PointToScreen(DGV.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, False).Location)

For case of this post:

Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As
  Dim DisplayPoint1 As Point = DGV.PointToScreen(DGV.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, False).Location)
ContextMenuStrip1.Show(DisplayPoint1)
End Sub

Upvotes: 6

matzone
matzone

Reputation: 5719

Try to change your code like this ..

Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick

    Dim columnName As String = DataGridView1.Columns(e.ColumnIndex).Name
    If columnName = "Image" Then 
       DataGridView1.CurrentCell = dgvDataDaftar.Rows(e.RowIndex).Cells(e.ColumnIndex)
       Me.Status_ContextMenuStrip1.Show(dgvDataDaftar, DataGridView1.PointToClient(Windows.Forms.Cursor.Position))
    End If

End Sub

Upvotes: 0

David -
David -

Reputation: 2031

Try the following code

Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
    Dim columnName As String = DataGridView1.Columns(e.ColumnIndex).Name

    If columnName = "Image" Then
        Dim RowHeight1 As Integer = DataGridView1.Rows(e.RowIndex).Height
        Dim CellRectangle1 As Rectangle = DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, False)

        CellRectangle1.X += DataGridView1.Left
        CellRectangle1.Y += DataGridView1.Top + RowHeight1

        Dim DisplayPoint1 As Point = PointToScreen(New Point(CellRectangle1.X, CellRectangle1.Y))

        ContextMenuStrip1.Show(DisplayPoint1)
    End If
End Sub

Upvotes: 8

Related Questions