user1231584
user1231584

Reputation: 203

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

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.

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

Upvotes: 20

Views: 40595

Answers (4)

Zhelyo Hristov
Zhelyo Hristov

Reputation: 21

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

Upvotes: 1

ary
ary

Reputation: 21

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

Upvotes: 2

Amritpal Singh
Amritpal Singh

Reputation: 1785

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.

Upvotes: 0

watbywbarif
watbywbarif

Reputation: 7017

You can use paygrid.PointToScreen() method.

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

Upvotes: 27

Related Questions