Question Datagridview cell position (.Net 2008).

priyamtheone

Well-known member
Joined
Sep 20, 2007
Messages
96
Programming Experience
Beginner
How can I get the position of a certain cell of a datagridview. To be specific I don't want the mouse coordinates. Suppose I have a datagridview with Name (ReadOnly), ID and TransDate columns and I want to popup a tiny lookup form when the mouse is clicked or Enter button is pressed when the focus is upon the Name column. The lookup form should openup exactly upon the Name column of the selected row. Thanks.
 
You know, I didn't know the answer to your question so I simply opened the MSDN Library documentation for the DataGridView and started looking down the list of members. Within 30 seconds I had found the GetCellDisplayRectangle method, described thusly:
Returns the rectangle that represents the display area for a cell.
Always look first and ask questions later. If you know what type or member you are dealing with, go straight to the documentation for that type or member. You won't always find what you want but many times you will, including this time.
 
Well jmcilhinney, I wasn't exactly aware of the member initially to go straight to the documentation. Anyway, here's a solution I came up to.
I am opening a dialog form above the datagridview form. In Form1
I have a datagridview containing few columns among which is 'TransDate' column having index 8. When I click on it, frmDate opens up exactly on it. My
revised code follows:

VB.NET:
Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles 
DataGridview1.CellClick
    If DataGridview1.CurrentCell.OwningColumn.Name = "TransDate" Then
        Dim objPoint As Point

        'Getting the exact location of the cell with respect to Form1.
        'Note- Me.Location is added here alongwith DataGridView1.Location
        'and DataGridView1.GetDisplayRectangle().Location.
        'This is done because the form frmDate we are calling is external to
        'Form1 and if we don't add Me.Location
        'here, frmDate won't open exactly at the desired point if we move or
        'resize Form1. Instead of frmDate if
        'we use any control inside Form1 to place over 'TransDate' column then
        'remove Me.Location here and the
        'next two lines.
        objPoint = (Me.Location + DataGridview1.Location +
        DataGridview1.GetCellDisplayRectangle(8,
        DataGridview1.CurrentCellAddress.Y, True).Location)

        'Setting the exact location where frmDate will be opened. The last
        'digits may vary according to your
        'requirement.
        objPoint.X = objPoint.X - 158
        objPoint.Y = objPoint.Y + 28
            
        objFrmDate = New frmDate()
        objFrmDate.Location = objPoint
        objFrmDate.ShowDialog(Me)
        objFrmDate = Nothing
    End If
End Sub

Regards.
 
I wasn't exactly aware of the member initially to go straight to the documentation.

No, you were looking for the member, which is exactly the point. You knew that you were using a DataGridView so you should have gone straight to the documentation for the DataGridView. There you would have found the appropriate member, i.e. GetCellDisplayRectangle. If some other time you are using a Listview and you want to know how to do something with it, go straight to the documentation for the ListView. Etc, etc.
 
Back
Top