2012-03-08 38 views
8

我有隱藏日曆的窗體窗體。我想在DataGridView的當前單元格下顯示該表單。位置根據當前單元的位置而改變。如何在DataGridView中獲取當前單元格的位置x和y?

我不希望當前的單元格或當前列,我想要的位置,以便我可以設置我的日期形式的位置。

這裏是我使用的是什麼,但它不工作:

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

回答

10

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

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

謝謝你這麼much..it工作完全 – user1231584 2012-03-08 15:25:46

0

您可以嘗試

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

和呼叫方法

Point pt; 
GetCursorPos(out pt); 

PT將提供x和y。

+0

謝謝,但我不想戈光標位置我想要datagridview中的當前單元格的位置。並且您已經從watbywbarif獲取了我的answear – user1231584 2012-03-08 15:27:52

1

你可以試試這個:

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

歡迎來到StackOverflow!請在您的代碼中添加一個簡短的解釋。爲什麼以及它如何工作? – Aurasphere 2015-11-13 10:56:26

0
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 
相關問題