How do I correctly position a Context Menu when I right click a DataGridView's column header?

前端 未结 9 1024
名媛妹妹
名媛妹妹 2020-12-29 02:58

I would like to extended DataGridView to add a second ContextMenu which to select what columns are visible in the gird. The new ContextMenu will be displayed on right click

9条回答
  •  無奈伤痛
    2020-12-29 03:13

    Calling Show twice will get you the exact location of the cursor. This answer is for those whom are unable to get the result with all above answers.

    private void MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Right)
        {
            contextMenuStrip.Show(dataGrid, e.Location));
            contextMenuStrip.Show(Cursor.Position);
        }
    }
    

提交回复
热议问题