DataGridView Mouse and ContextMenu event

use the context menu opening event. This way the position of the contect menu is at the mouseposition automatically. The cell iss selected to get the rowpointer moved. The the row gets selected


private void cmuGrid_Opening(object sender, CancelEventArgs e)

{

    // get the mouse porsition and convert it to coordinate within the grid

    Point pt = grdJobs.PointToClient(MousePosition);

     

    // now get the testinfo at the gridcoordinate

    DataGridView.HitTestInfo hti = grdJobs.HitTest(pt.X, pt.Y);

     

    // is it a cell?

    if (hti.Type == DataGridViewHitTestType.Cell)

    {

        // make the cell the current on and select the row

        grdJobs.ClearSelection();

        grdJobs.CurrentCell = grdJobs.Rows[hti.RowIndex].Cells[hti.ColumnIndex];

        grdJobs.Rows[hti.RowIndex].Selected = true;

         

        // do whatever you want to do, set contextmenu text for example

         

    }

}