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

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

    Here is a very simple way to make context menu appear where you right-click it.

    Handle the event ColumnHeaderMouseClick

    private void grid_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) 
    {
      if (e.Button == System.Windows.Forms.MouseButtons.Right)
        contextMenuHeader.Show(Cursor.Position);
    }
    

    contextMenuHeader is a ContextMenuStrip that can be defined in the Designer view or at runtime.

提交回复
热议问题