DataGridView: Get row at point

Hi,

I would like to have a row selected when i right click on a control. However, i cannot find how to get the row based on the point of the mouse_up event.

I searched the docs, but could not find any member of datagridview that could help me with that. I think i'm overlooking something.

Ward


Answer this question

DataGridView: Get row at point

  • Isaac1

    Does anyone know how to do the above in .NET 2003 using the normal DataGrid   I'm a VB guy but I understand C# too...

    Thanks...

  • saj14saj

    Take a look at the DataGridView.HitTest(int x, int y) method.
    The x and y variables are coordinates that you can easily retrieve via the Mouse property
    This returns a HitTestInfo object that contians info such as rownumber and columnnumber

    goodluck !

  • Offir

    Hi Handi,

    Right-click does not automatically select the row (selectmode is currently fullrow), so currentcell does not have the needed value.

    Ward



  • smokeb

    dataGridView1.CurrentCell

  • Chris Mullins

    The same (from the docs):

       System.Windows.Forms.DataGrid.HitTestInfo myHitTest;
       // Use the DataGrid control's HitTest method with the x and y properties.
       myHitTest = dataGrid1.HitTest(e.X,e.Y);
       Console.WriteLine(myHitTest);
       Console.WriteLine("Column " + myHitTest.Column);
       Console.WriteLine("Row " + myHitTest.Row);
       Console.WriteLine("Type " + myHitTest.Type);
       Console.WriteLine("ToString " + myHitTest.ToString());
       Console.WriteLine("Hit " + myHitTest.Type.ToString());

    Ward



  • R_s

    Thanks a lot de_henny.

    Ward

  • DataGridView: Get row at point