DataGridView right-click menu/copy example?

后端 未结 2 974
生来不讨喜
生来不讨喜 2021-02-03 10:53

I have a DataGridView (dgv1) on my form. In a particular cell, I\'d like for the user to be able to right-click and choose \"COPY\" to copy the contents of the cell to the clip

2条回答
  •  别那么骄傲
    2021-02-03 11:43

    You can use ContextMenuStrip to accomplish this. (Or ContextMenu for pre-VS2k5)

    Excerpt from this article:

    ContextMenuStrip mnu = new ContextMenuStrip();
    ToolStripMenuItem mnuCopy = new ToolStripMenuItem("Copy");
    ToolStripMenuItem mnuCut = new ToolStripMenuItem("Cut");
    ToolStripMenuItem mnuPaste = new ToolStripMenuItem("Paste");
    //Assign event handlers
    mnuCopy.Click += new EventHandler(mnuCopy_Click);
    mnuCut.Click += new EventHandler(mnuCut_Click);
    mnuPaste.Click += new EventHandler(mnuPaste_Click);
    //Add to main context menu
    mnu.Items.AddRange(new ToolStripItem[] { mnuCopy, mnuCut, mnuPaste});
    //Assign to datagridview
    dataGridView1.ContextMenuStrip = mnu;
    

    There is more information on the above link.

提交回复
热议问题