19

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);
Simon
  • 6,062
  • 13
  • 60
  • 97
user1231584
  • 193
  • 1
  • 1
  • 4

4 Answers4

25

You can use paygrid.PointToScreen() method.

form_date.Location = paygrid.PointToScreen(
   paygrid.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).Location);
watbywbarif
  • 6,487
  • 8
  • 50
  • 64
  • I dont know who accepted this as answer, but it does not work as intendet... – Wolfgang Roth Nov 29 '22 at 17:49
  • Lol, this has worked for sure when it was written 10 years ago. Maybe you should ask again and specify what tool versions are you using and what do you get and what do you expect with your current experiments. – watbywbarif Dec 06 '22 at 12:21
  • Hello, i am using something like that: Rectangle r = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false); frm.Location = dataGridView1.PointToScreen(r.Location); DialogResult dr = frm.ShowDialog(this); Hmmm, looks like the same than yours... I must have done something different when writing the first comment... – Wolfgang Roth Dec 06 '22 at 17:15
2

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
Aurasphere
  • 3,841
  • 12
  • 44
  • 71
ary
  • 21
  • 1
1
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
0

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.

Skaiward
  • 3
  • 3
Amritpal Singh
  • 1,765
  • 1
  • 13
  • 20
  • Thanks but i dont want to ge the position of cursor i want the position of current cell in datagridview..and u have already got my answear from watbywbarif – user1231584 Mar 08 '12 at 15:27