Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
How to: Enable Users to Copy Multiple Cells to the Clipboard from the Windows Forms DataGridView Control

How to: Enable Users to Copy Multiple Cells to the Clipboard from the Windows Forms DataGridView Control

When you enable cell copying, you make the data in your DataGridView control easily accessible to other applications through the Clipboard. The values of the selected cells are converted to strings and added to the Clipboard as tab-delimited text values for pasting into applications like Notepad and Excel, and as an HTML-formatted table for pasting into applications like Word.

You can configure cell copying to copy cell values only, to include row and column header text in the Clipboard data, or to include header text only when users select entire rows or columns.

Depending on the selection mode, users can select multiple disconnected groups of cells. When a user copies cells to the Clipboard, rows and columns with no selected cells are not copied. All other rows or columns become rows and columns in the table of data copied to the Clipboard. Unselected cells in these rows or columns are copied as blank placeholders to the Clipboard.

To enable cell copying

The following complete code example demonstrates how cells are copied to the Clipboard. This example includes a button that copies the selected cells to the Clipboard using the DataGridView.GetClipboardContent method and displays the Clipboard contents in a text box.

using System;
using System.Windows.Forms;

public class Form1 : Form
    private DataGridView DataGridView1 = new DataGridView();
    private Button CopyPasteButton = new Button();
    private TextBox TextBox1 = new TextBox();

    public static void Main()
        Application.Run(new Form1());

    public Form1()
        this.DataGridView1.AllowUserToAddRows = false;
        this.DataGridView1.Dock = DockStyle.Fill;

        this.CopyPasteButton.Text = "copy/paste selected cells";
        this.CopyPasteButton.Dock = DockStyle.Top;
        this.CopyPasteButton.Click += new EventHandler(CopyPasteButton_Click);

        this.TextBox1.Multiline = true;
        this.TextBox1.Height = 100;
        this.TextBox1.Dock = DockStyle.Bottom;

        this.Load += new EventHandler(Form1_Load);
        this.Text = "DataGridView Clipboard demo";

    private void Form1_Load(object sender, System.EventArgs e)
        // Initialize the DataGridView control. 
        this.DataGridView1.ColumnCount = 5;
        this.DataGridView1.Rows.Add(new string[] { "A", "B", "C", "D", "E" });
        this.DataGridView1.Rows.Add(new string[] { "F", "G", "H", "I", "J" });
        this.DataGridView1.Rows.Add(new string[] { "K", "L", "M", "N", "O" });
        this.DataGridView1.Rows.Add(new string[] { "P", "Q", "R", "S", "T" });
        this.DataGridView1.Rows.Add(new string[] { "U", "V", "W", "X", "Y" });
        this.DataGridView1.ClipboardCopyMode = 

    private void CopyPasteButton_Click(object sender, System.EventArgs e)
        if (this.DataGridView1
            .GetCellCount(DataGridViewElementStates.Selected) > 0)
                // Add the selection to the clipboard.

                // Replace the text box contents with the clipboard text. 
                this.TextBox1.Text = Clipboard.GetText();
            catch (System.Runtime.InteropServices.ExternalException)
                this.TextBox1.Text = 
                    "The Clipboard could not be accessed. Please try again.";


This code requires:

  • References to the N:System and N:System.Windows.Forms assemblies.

For information about building this example from the command line for Visual Basic or Visual C#, see Building from the Command Line (Visual Basic) or Command-line Building With csc.exe. You can also build this example in Visual Studio by pasting the code into a new project. Also see How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio.

© 2015 Microsoft