vb.net 在 DataGridView 中获取“当前单元格”的 X/Y 坐标?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17557951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:59:18  来源:igfitidea点击:

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

vb.netwinformsvisual-studio-2010datagridview

提问by NotQuiteThereYet

[using VB 2010 / Winforms]

[使用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.

我有一个包含几列的 DataGridView。它是未绑定的,没有连接到任何类型的数据库或任何东西——我只是根据用户输入逐个单元地填充它。

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

所以无论如何,DGV 中的列之一是“图像”类型(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).

当我运行上面的代码并单击图像单元格时,会出现上下文菜单,但它出现在屏幕的左上角。如何让它出现在单击的单元格所在的确切位置?我实际上希望它出现在单击的单元格下方,以便它具有与组合框“下拉”类似的视觉效果(并且我知道如何在我能弄清楚如何偏移 X 和 Y 坐标时把它放在它需要的地方附近)。

Thanks!

谢谢!

回答by David -

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

回答by LuckyLuke82

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

回答by matzone

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