DataGridView Cell location problem

kriswinner

Member
Joined
Apr 23, 2009
Messages
23
Programming Experience
10+
I'm using the DataGridView control to display a table of textboxes and comboboxes. In one of the combobox columns, I need to popup a form when that cell is (left) clicked. I have that portion working.

The part that I have issue with is locating the popup form properly.
In the _CellClick event, I test to see which column was clicked. If it was the appropriate column, I capture the .MousePosition.X and .Y position and set the form's location based on that. As you would expect, the form is displayed at the exact location that I click the cell. I would like to be able to have the form display in the same location relative to the cell regardless of where in the cell I click.

Is there somewhere I can get the location of one of the corners of the cell (a "fixed" point) that I can use to consistently position the form?
 
Have a look at DataGridView.GetCellDisplayRectangle Method.
 
Excellent!

Works great.

A short version of what it became.....
table_Units is a datagridview.
frm_ShuttlePopup is a form that I use as a popup menu that is more complex than a typical context menu.


VB.NET:
Dim rect As Rectangle                    
rect = table_Units.GetCellDisplayRectangle(colNum, rowNum,1)
popup_x = rect.X + rect.Width / 2
popup_y = rect.Y + (rect.Height / 2) + 92
frm_ShuttlePopup.Top = popup_y
frm_ShuttlePopup.Left = popup_x
frm_ShuttlePopup.Show()

Note that I added 92 to the y direction to get the form where I wanted.


Thanks for the quick reply!
 
If the "92" value is related to the position of the DGV control you can use the PointToScreen method instead like this:
VB.NET:
Dim r As Rectangle = Me.DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, False)
r.Location = Me.DataGridView1.PointToScreen(r.Location)

'r is now a screen rectangle
demausdauth said:
ContentBounds
That will not work, it returns the bounds of the content of the cell, content location is also relative to cell and content size is usually different than the display size of cell, ContentBounds has no information about the cell.
 

Latest posts

Back
Top