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

前端 未结 9 1025
名媛妹妹
名媛妹妹 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:27

    You were nearly right. You just need to the apply the PointToScreen method to the calling control:

    private void ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Right)
        {
            contextMenuStrip.Show(((DataGridView)sender).PointToScreen(e.Location));
        }
    }
    

    I think this is the most elegant solution, because it uses only the ColumnHeaderMouseClick arguments and not Cursor.Position.

提交回复
热议问题