简体   繁体   中英

Copy datagridview cell content to clipboard in FullRowSelect Mode

I have a winform datagridview to show customer details and it has a context menu. And I have set the datagridview selection Mode to "FullRowSelect". What i want is i want to copy the content of the clicked cell content to the clipboard. Not the whole row content. Just the cell content.

I have used the following code to show the Context Menu when it right click on the datagridview and select the row.

private void dgvCusList_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
    {
        if (e.RowIndex != -1 && e.ColumnIndex != -1)
        {
            if (e.Button == MouseButtons.Right)
            {
                DataGridViewCell clickedCell = (sender as DataGridView).Rows[e.RowIndex].Cells[e.ColumnIndex];

                this.dgvCusList.CurrentCell = clickedCell;  

                var relativeMousePosition = dgvCusList.PointToClient(Cursor.Position);

                this.cnxtMnuCusResult.Show(dgvCusList, relativeMousePosition);
            }
        }
    } 

I want to copy the the cell content to clipboard when i click copy menu item in my context menu. Please help me to solove this matter. Thanks in advance. :)

If you are having the SelectionMode property as FullRowSelect then copy functionality of DataGridView will copy the entire row. Change the value to CellSelect . Set the below properties to copy only the cell content.

dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
dataGridView1.MultiSelect = false;

If you want to retain the FullRowSelect selection mode, then do like below..

 void contextMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {
        if (e.ClickedItem.Text == "Copy" && dataGridView1.CurrentCell.Value != null)
        {
            Clipboard.SetDataObject(dataGridView1.CurrentCell.Value.ToString(), false);
        }
    }

 void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right && e.ColumnIndex != -1 && e.RowIndex != -1)
        {
            dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ContextMenuStrip = contextMenu;
            dataGridView1.CurrentCell = myDGV.Rows[e.RowIndex].Cells[e.ColumnIndex];
        }
    }

In contextMenu or CellDoubleClick Event or cellMouseClick event , you can do the following

Clipboard.SetText(dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString());

It will copy the particular cell's Content

Junaith's answer does not work like it should when 'EditMode' = EditOnEnter; it only really works when it is set to EditProgrammatically. An additional step then may be for some, to replace the default menu with your own. You can disable the default menu:

(In VB)

Private Sub DG1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DG1.EditingControlShowing

        DirectCast(e.Control, DataGridViewTextBoxEditingControl).ShortcutsEnabled = False
End Sub

most easy solution: add CellBeginEdit and CellEndEdit event to control ClipboardCopyMode.

private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e) 
    { 
     this.dataGridView1.ClipboardCopyMode = DataGridViewClipboardCopyMode.Disable; 
    } 

    private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) 
    { 
     this.dataGridView1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText; 
    } 

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