Search code examples
c#.netwinformsdatagridviewcontextmenustrip

Keep context location change with DatagridView scroll


I have a datagrid View and code is

 DataGridView m_ClientProcessDataGridView = new DataGridView();

And I have a context menu and it is

 ContextMenuStrip contextMenuStripForCells;

on right click on datagrid and showing context menu as

contextMenuStripForCells.Show(m_ClientProcessDataGridView.PointToScreen(e.Location));

The issue is when I scroll datagridview context menu will not move with row position, do we have any idea to keep it move with scroll?

Or can I disable datagridview scroll when context is open ?


Solution

  • Scroll by wheel happens in OnMouseWheel. You can override the method and check if the ContextMenuStrip is open, prevent scroll:

    public class MyDataGridView : DataGridView
    {
        protected override void OnMouseWheel(MouseEventArgs e)
        {
            if (this.ContextMenuStrip != null && this.ContextMenuStrip.Visible)
                return;
            base.OnMouseWheel(e);
        }
    }