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


我有一个Windows表单,其中隐藏着日历。我想在DataGridView的当前单元格下面显示表单。位置根据当前单元格的位置而变化。

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

这是我正在使用但它不起作用:

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);

1648
2018-03-08 15:04


起源



答案:


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

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

13
2018-03-08 15:12



非常感谢你..完美的工作 - user1231584


答案:


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

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

13
2018-03-08 15:12



非常感谢你..完美的工作 - user1231584


你可能想试试这个:

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

2
2017-11-13 10:45



欢迎来到StackOverflow!请在您的代码中添加简要说明。为什么以及如何运作? - Aurasphere


你可以试试

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

并调用方法为

Point pt;
GetCursorPos(out pt);

pt将提供x和y。


0
2018-03-08 15:13



谢谢,但我不想ge光标的位置我想在datagridview中的当前单元格的位置..你已经从watbywbarif我的answear - user1231584


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

所以你可以使用:

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

0
2017-11-14 09:20