C# 如何在 DataGridView 中获取当前单元格位置 x 和 y?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9619746/
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-08-09 08:12:09  来源:igfitidea点击:

How do I to get the current cell position x and y in a DataGridView?

c#.netdatagridview

提问by user1231584

I have a Windows form with a calendar which is hidden. I want to show the form right under the current cell of a DataGridView. The position changes according to the position of current cell.

我有一个带有隐藏日历的 Windows 窗体。我想在 DataGridView 的当前单元格下显示表单。位置根据当前单元格的位置而变化。

I don't want the current cell or current column, I want the position so that I can set the location of my date forms.

我不想要当前单元格或当前列,我想要位置以便我可以设置日期表单的位置。

Here is what I am using but its not working:

这是我正在使用但不起作用的内容:

int po_X = paygrid.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).Left+paygrid.Left;
int po_Y = paygrid.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).Bottom+paygrid.Top;
form_date.Location = new System.Drawing.Point(po_X, po_Y);

采纳答案by watbywbarif

You can use paygrid.PointToScreen() method.

您可以使用 paygrid.PointToScreen() 方法。

form_date.Location = paygrid.PointToScreen(
   paygrid.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).Location);

回答by Amritpal Singh

You may try

你可以试试

[DllImport("user32.dll", EntryPoint = "GetCursorPos")]
private static extern bool GetCursorPos(out Point lpPoint);

and call method as

并调用方法为

Point pt;
GetCursorPos(out pt);

pt will provide x and y.

pt 将提供 x 和 y。

回答by ary

You may want to try this:

你可能想试试这个:

Lookup_Account.Left = datagrid_setting.GetCellDisplayRectangle(colIndex, rowIndex, False).Left
Lookup_Account.Top = datagrid_setting.GetCellDisplayRectangle(colIndex, rowIndex, False).Top

回答by Zhelyo Hristov

Rectangle cellRect = paygrid.GetCellDisplayRectangle(e.ColumnIndex, 
e.RowIndex, true);

So You can use:

所以你可以使用:

cellRect.X - for X position, cellRect.Y - for Y position,
cellRect.Width - for column width and cellRect.Height - for column height