Skip to content

Datagridview copy cell to clipboard #4832

Closed

Description

@itg-assistant

Vincent asked 2025-08-18 18:20:41 UTC

Hi!

Quick question (hopefully)

How do I copy the contents of a specific cell to clipboard when pressing ctrl-c ? I've tried the keypress event and also tried setting the ClipboardCopyMode to "ExludeUnselectedColumns". Whatever I try I always copy the whole row with column headers regardless of the ClipboardCopyMode setting.

I also tried handling the KeyPress event and if the key was "c" then if My.Computer.Keyboard.CtrlKeyDown = True but this always returns false (My.Computer.Keyboard.CtrlShiftDown works though)

Can you help?

Thanks!

Vincent

Activity

  1. itg-assistant commented on Jun 16, 2026

    @itg-assistant
    Author

    Julie (ITG) answered 2025-08-19 15:11:52 UTC

    You can do this by using accelerators and the ClientClipboard extension.

    Make sure to install the ClientClipboard extension using nuget.

    To create an accelerator, open the Page in the designer and go the the Page properties. Click on the 3 dots by "Accelerators" and click "Add". Click on the dropdown arrow and click the box for ctrl and set the key to C. See attached picture.

    Once that's set up, go to the lightning bolt to open the events for the page. Add an "Accelerator" event. It should be called Accelerator_CtrlC

    Then paste this code in the accelerator event:

    private async void Accelerator_CtrlC(object sender, AcceleratorEventArgs e)
    {
    if (dataGridView1.CurrentCell != null)
    {
    var value = dataGridView1.CurrentCell.Value?.ToString();
    await ClientClipboard.WriteTextAsync(value);
    }
    }
    

    I've attached a working sample here.

  2. itg-assistant commented on Jun 16, 2026

    @itg-assistant
    Author

    Julie (ITG) answered 2025-08-19 15:19:36 UTC

    Note that instead of var value = dataGridView1.CurrentCell.Value?.ToString(); you can do string value = Convert.ToString(dataGridView1.CurrentCell.Value);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    GeneralQuestions, how-tos and everything else.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions