如何在右键单击datagridview单元格时显示上下文菜单

时间:2017-08-08 17:51:21

标签: .net winforms powershell datagridview contextmenu

我有一个带有数据网格的表单。我想要做的是当我右键单击一个单元格时,鼠标旁边会出现一个下拉菜单。我需要能够从菜单中选择一个选项并运行一些东西。

我可以获得正确的点击事件进行注册,但上下文菜单不会出现。 下面的代码中的菜单应该带有复制单元格的选项。

[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Drawing”)
$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(1040,518)
$form.KeyPreview = $true
$form.StartPosition = 'centerscreen'
$form.BackColor = 'MidnightBlue'
$form.Add_KeyDown({if($_.KeyCode -eq "Escape"){$form.Close()}})
$form.Text = "VIOC Toolkit 5.4" 
$form.Icon = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + "\powershell_ise.exe")
$form.MinimumSize = New-Object System.Drawing.Size(1040,525)

$DataGrid1 = New-Object System.Windows.Forms.DataGridView
$DataGrid1.Location = New-Object System.Drawing.Size(298,29)
$DataGrid1.Dock = "Fill"
$DataGrid1.BorderStyle = 'FixedSingle'    
$DataGrid1.DefaultCellStyle.Font = New-Object System.Drawing.Font($dgfont,$dgfontSize)
$DataGrid1.AlternatingRowsDefaultCellStyle.BackColor = 'LightGray'
$DataGrid1.AllowUserToAddRows = $false
$DataGrid1.RowHeadersVisible = $false
$DataGrid1.BackgroundColor = "White"
$DataGrid1.Name="DataGrid1"
$DataGrid1.Text="DataGrid1"
$DataGrid1.ColumnCount = 3
$DataGrid1.Columns[0].Name = 'one'
$DataGrid1.Columns[1].Name = 'two'
$DataGrid1.Columns[2].Name = 'three'
$DataGrid1.Rows.add(@('a', 'b', 'c'))
$DataGrid1.Rows.add(@('d', 'e', 'f'))
#*************************************************************#    
$DataGrid1.add_CellMouseClick({
    if($_.button -eq 'Right'){
        ContextMenu($DataGrid1)
    }
})

function ContextMenu($Grid){
    #create the context menu
    $menu = New-Object System.Windows.Forms.ContextMenu
    $CopyMenuItem = New-Object System.Windows.Forms.MenuItem
    $copymenuItem.text = 'copy'
    $copymenuItem.add_Click({write-host 'clicked copy'})
    $menu.MenuItems.AddRange(@($CopyMenuItem))

}

#***************************************************************#
$form.Controls.Add($DataGrid1)
$form.ShowDialog() | out-null

2 个答案:

答案 0 :(得分:1)

试试这段代码:

[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Drawing”)
$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(1040,518)
$form.KeyPreview = $true
$form.StartPosition = 'centerscreen'
$form.BackColor = 'MidnightBlue'
$form.Add_KeyDown({if($_.KeyCode -eq "Escape"){$form.Close()}})
$form.Text = "VIOC Toolkit 5.4" 
$form.Icon = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + "\powershell_ise.exe")
$form.MinimumSize = New-Object System.Drawing.Size(1040,525)

[System.Windows.Forms.DataGridView] $DataGrid1 = New-Object System.Windows.Forms.DataGridView
$DataGrid1.Location = New-Object System.Drawing.Size(298,29)
$DataGrid1.Dock = "Fill"
$DataGrid1.BorderStyle = 'FixedSingle'    
#$DataGrid1.DefaultCellStyle.Font = New-Object System.Drawing.Font $dgfont,$dgfontSize
$DataGrid1.AlternatingRowsDefaultCellStyle.BackColor = 'LightGray'
$DataGrid1.AllowUserToAddRows = $false
$DataGrid1.RowHeadersVisible = $false
$DataGrid1.BackgroundColor = "White"
$DataGrid1.Name="DataGrid1"
$DataGrid1.Text="DataGrid1"
$DataGrid1.ColumnCount = 3
$DataGrid1.Columns[0].Name = 'one'
$DataGrid1.Columns[1].Name = 'two'
$DataGrid1.Columns[2].Name = 'three'
$DataGrid1.Rows.add(@('a', 'b', 'c'))
$DataGrid1.Rows.add(@('d', 'e', 'f'))

#Creation of content click event
$ClickElementMenu=
{
    [System.Windows.Forms.ToolStripItem]$sender = $args[0]
    [System.EventArgs]$e= $args[1]

    $Contentcell=$DataGrid1.Rows[$DataGrid1.CurrentCell.RowIndex].Cells[$DataGrid1.CurrentCell.ColumnIndex].Value
    $ElementMenuClicked=$sender.Text
    $RowIndex=$DataGrid1.CurrentCell.RowIndex
    $ColIndex=$DataGrid1.CurrentCell.ColumnIndex


    $result="Click on element menu : '{0}' , in rowindex : {1} , column : {2}, content cell : {3}" -f $ElementMenuClicked,  $RowIndex, $ColIndex, $Contentcell;
    Write-Host $result
}

#creation menu
$contextMenuStrip1=New-Object System.Windows.Forms.ContextMenuStrip

#creation element1 of menu
[System.Windows.Forms.ToolStripItem]$toolStripItem1 = New-Object System.Windows.Forms.ToolStripMenuItem
$toolStripItem1.Text = "Element 1";
$toolStripItem1.add_Click($ClickElementMenu)
$contextMenuStrip1.Items.Add($toolStripItem1);

#creation element2 of menu
[System.Windows.Forms.ToolStripItem]$toolStripItem2 = New-Object System.Windows.Forms.ToolStripMenuItem
$toolStripItem2.Text = "Element 2";
$toolStripItem2.add_Click($ClickElementMenu)
$contextMenuStrip1.Items.Add($toolStripItem2);

#creation event of mouse down on datagrid and show menu when click
$DataGrid1.add_MouseDown({
    $sender = $args[0]
    [System.Windows.Forms.MouseEventArgs]$e= $args[1]

    if ($e.Button -eq  [System.Windows.Forms.MouseButtons]::Right)
    {
        [System.Windows.Forms.DataGridView+HitTestInfo] $hit = $DataGrid1.HitTest($e.X, $e.Y);
        if ($hit.Type -eq [System.Windows.Forms.DataGridViewHitTestType]::Cell)
        {
            $DataGrid1.CurrentCell = $DataGrid1[$hit.ColumnIndex, $hit.RowIndex];
            $contextMenuStrip1.Show($DataGrid1, $e.X, $e.Y);
        }

    }
})




#***************************************************************#
$form.Controls.Add($DataGrid1)
$form.ShowDialog() | out-null

答案 1 :(得分:-2)

这是一个最小的C#示例

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace DataGridViewContextMenu_45574948
{
    public partial class Form1 : Form
    {
        BindingList<dgvitem> dgvitems = new BindingList<dgvitem>();
        DataGridView dgv = new DataGridView();

        public Form1()
        {
            InitializeComponent();
            InitializeTheGrid();
        }

        private void InitializeTheGrid()
        {
            dgv.AutoGenerateColumns = true;
            dgv.DataSource = dgvitems;
            dgv.CellMouseClick += Dgv_CellMouseClick;
            this.Controls.Add(dgv);
        }

        private void Dgv_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                ContextMenu cm = new ContextMenu();
                MenuItem mi = new MenuItem();
                mi.Text = "Menu option 1";
                mi.Click += MenuItem1Click;
                cm.MenuItems.Add(mi);
                mi = new MenuItem();
                mi.Text = "Menu option 2";
                mi.Click += MenuItem2Click;
                cm.MenuItems.Add(mi);
                cm.Show(dgv, new Point(e.Location.X + 10, e.Location.Y + 10));
            }
        }

        private void MenuItem2Click(object sender, EventArgs e)
        {
            //do something here
        }

        private void MenuItem1Click(object sender, EventArgs e)
        {
            //do something here
        }
    }

    public class dgvitem
    {
        public string col1 { get; set; }
        public string col2 { get; set; }
        public string col3 { get; set; }
    }
}