简体   繁体   中英

How to copy data to clipboard when user selects “Copy” from ContextMenu

How would you copy the contents of a datagrid cell to the clipboard when the user selects copy from the context menu on a paticular cell?

public Form1()
{
    InitializeComponent();
    dataGridView1.MouseClick += dataGridView1_MouseClick;
}

void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        ContextMenu m = new ContextMenu();
        m.MenuItems.Add(new MenuItem("Copy"));

        int currentMouseOuverRow = dataGridView1.HitTest(e.X, e.Y).RowIndex;

        m.Show(dataGridView1, new Point(e.X, e.Y));
    }
}

I guess what I'd do is this:

var hitTestInfo = dataGridView1.HitTest(e.X, e.Y);
if (hitTestInfo.Type != DataGridViewHitTestType.Cell) { return; }

var mi = new MenuItem("Copy")
mi.Tag = hitTestInfo;
mi.Click += (s, e) =>
{
    var hti = ((MenuItem)s).Tag as HitTestInfo;
    var val = dataGridView1.Rows[hti.RowIndex].Cells[hti.ColumnIndex].Value;

    Clipboard.SetData(DataFormats.Text, val);
}

m.MenuItems.Add(mi);

You could alter your MouseClick-Method like this:

private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        using (ContextMenu m = new ContextMenu())
        {
            MenuItem mItem = new MenuItem("Copy");
            m.MenuItems.Add(mItem);

            DataGridView.HitTestInfo information = dataGridView1.HitTest(e.X, e.Y);

            try
            {
                dataGridView1.CurrentCell = dataGridView1.Rows[information.RowIndex].Cells[information.ColumnIndex];
                m.Show(dataGridView1, new Point(e.X, e.Y));
                mItem.Click += mItem_Click;
            }
            catch (Exception)
            {

            }
        }
    }
}

After that you create a method which is called when the mItem.Click-Event is raised:

void mItem_Click(object sender, EventArgs e)
{
    Clipboard.SetText(dataGridView1.CurrentCell.Value.ToString());
}

Why not add a Click event handler to your menu item? Then all you need to do in that event is the following:

private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
    if (dataGridView1.SelectedRows.Count < 1)
        return;

    var cell = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells["CellName"].Value;
    if (cell != null)
        Clipboard.SetText(cell.ToString());
}

Just added a quick edit to make sure there's actually a value in the cell before you go and overwrite whatever's on the clipboard.

You can use the Clipboard class, in this particular case you want the SetText method.
Note: the msdn says that Clipboard is in System.Windows however on my machine it is in System.Windows.Forms.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM