locked
Find location of cell on a form RRS feed

  • Question

  • Hello, I cannot figure out how to get a dataGridViewCell's location on the form.  

    I've created a custom DataGridViewColumn and when the user clicks the column I need to locate the columns Parent control which is a DataGridView and then I need the DGV's parent which will be a form... I then need to figure out the location of a particular cell on the Parent form so I can place a dialgue box there.
    Thursday, August 27, 2009 6:06 PM

Answers

  • I actually finally figured out how to do it.  Here is the solution:

    // This gives the top left corner point of the cell relative to the DGV.
    Rectangle rect = parentDgv.GetCellDisplayRectangle(parentDgv.CurrentCell.ColumnIndex, parentDgv.CurrentCell.RowIndex, true);
    
    //then to get the point relative to the form I go
    Point loc = new Point(rect.X + parentDgv.Location.X, rect.Y + parentDgv.Location.Y);

    • Marked as answer by CommanderKeen Thursday, August 27, 2009 7:43 PM
    Thursday, August 27, 2009 7:42 PM

All replies

  • The DataGridViewColumn type has a Width property.
    The DataGridViewRow type has a Height property.
    The DataGridViewCell has a Size property, in pixels. 

    If you know the clicked cell's location in the DGV, then those properties should allow you to determine a Point object.

    Point screenLocation DataGridView.PointToScreen(point);  // locate your dialog box at screenLocation

    Mark the best replies as answers. "Fooling computers since 1971."
    Thursday, August 27, 2009 6:25 PM
  • I don't know how to get the particular location of the cell of ultragridview. i think it will be tough but there is an alternative option available which i did when i need same functionality. what i did was, I implemented the mousemove event of the mouse and there i store the mouse location in my class level variable. Then i get that event where user click on my desire cell of grid, I have the points[Get from mouse move] which i needed to show a form by passing the last get location of mouse. but i used the infragistic grid but i think this makes no difference.
    • Proposed as answer by Rudedog2 Thursday, August 27, 2009 6:34 PM
    Thursday, August 27, 2009 6:28 PM
  • I don't know how to get the particular location of the cell of ultragridview. i think it will be tough but there is an alternative option available which i did when i need same functionality. what i did was, I implemented the mousemove event of the mouse and there i store the mouse location in my class level variable. Then i get that event where user click on my desire cell of grid, I have the points[Get from mouse move] which i needed to show a form by passing the last get location of mouse. but i used the infragistic grid but i think this makes no difference.


    You cheated.  You fooled it.   I like it!

    Mark the best replies as answers. "Fooling computers since 1971."
    Thursday, August 27, 2009 6:34 PM
  • Try the following sample code . if you want to do this
    // Form Mouse Move event....
    Private Sub Form1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    {
         m_MousePoint.x=e.x;
        m_MousePoint.Y=e.Y;
     }

    //Cell Click Event
    private void DataGridView1_CellClick(Object sender,DataGridViewCellEventArgs e)
    {
          if(e.ColumnIndex==yourdesirecolumnIndex)
         {
              //        use here m_MousePoint
         }
     }
    Thursday, August 27, 2009 6:40 PM
  • You cheated.  You fooled it.   I like it!

    "Fooling computers since 1971."
    @Rudedog2 ! I like your signature very much.I thought programming is something to solve your problem.
    Thursday, August 27, 2009 6:41 PM
  • Thanks for the replies guys:

    AUmidh I don't think this will work for my particular case because I need to get the location from code placed within my custom cell.

    RudeDog:

    I thought of this but I feel as though it's heavy handed....   Since a Cell only maintains the row index and column index I would have to find the width of each column up to the current column and then add the sum of the width of each column and likewise for the row... then I would have to add this to the location of the datagrid to location the cell relative to the dgv....

    I guess I don't have a problem with that if there's no other way...
    Thursday, August 27, 2009 6:56 PM
  • Thanks for the replies guys:

    AUmidh I don't think this will work for my particular case because I need to get the location from code placed within my custom cell.
    its not concern with the custom cell or any other. what my objective was to display a form (top,left position) where the mouse is click on one of my particular cell.

    clear me your objective then i will told you that if it works or not.

    and clarify "code placed within my custom cell."
    Thursday, August 27, 2009 7:01 PM
  • What I mean to clarify.

    I have a custom control that is embedded in a DataGridView Custom Cell.  So the control itself has to figure out where it is, therfore I cannot place a mouse move event because it will only pertain to when the mouse is over the Custom Control.

    I hope this makes sense.  Thx again.
    Thursday, August 27, 2009 7:20 PM
  • What I mean to clarify.

    I have a custom control that is embedded in a DataGridView Custom Cell.  So the control itself has to figure out where it is.

    So what is the problem then ? because i respond to this question on the basis of your following statement,

    "I then need to figure out the location of a particular cell on the Parent form so I can place a dialgue box there"

    and by doing that you can get the location within the cell when some one click with in the cell.
    Thursday, August 27, 2009 7:31 PM
  • I actually finally figured out how to do it.  Here is the solution:

    // This gives the top left corner point of the cell relative to the DGV.
    Rectangle rect = parentDgv.GetCellDisplayRectangle(parentDgv.CurrentCell.ColumnIndex, parentDgv.CurrentCell.RowIndex, true);
    
    //then to get the point relative to the form I go
    Point loc = new Point(rect.X + parentDgv.Location.X, rect.Y + parentDgv.Location.Y);

    • Marked as answer by CommanderKeen Thursday, August 27, 2009 7:43 PM
    Thursday, August 27, 2009 7:42 PM