# DataGridView Mouse and ContextMenu event

**URL:** <https://forums.thehomestack.io/t/datagridview-mouse-and-contextmenu-event/371>\
**Category:** vb.Net\
**Created:** [July 10, 2010, 3:25pm UTC](https://forums.thehomestack.io/t/datagridview-mouse-and-contextmenu-event/371 "2010-07-10T15:25:37Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://forums.thehomestack.io/uploads/default/original/1X/161426793bc0d8bdf0d1a6692b3e44446861cc72.jpeg) [@system](https://forums.thehomestack.io/u/system)\
**Post date:** [July 10, 2010, 3:25pm UTC](https://forums.thehomestack.io/t/datagridview-mouse-and-contextmenu-event/371/1 "2010-07-10T15:25:37Z")

</div>

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

```auto

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

         

    }

}

```
