Click here to Skip to main content
11,409,450 members (35,865 online)
Click here to Skip to main content

Tagged as

Copy Paste in Datagridview Control

, 8 Jun 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
How to copy paste in Datagridview control


This post discusses how to add a datagridview and a contextmenustrip with cut, copy and paste into the form.

Image 1

Populate the DataGridView and define the click events as follows:

private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    if (dataGridView1.SelectedCells.Count > 0)
        dataGridView1.ContextMenuStrip = contextMenuStrip1;

private void cutToolStripMenuItem_Click(object sender, EventArgs e)
    //Copy to clipboard

    //Clear selected cells
    foreach (DataGridViewCell dgvCell in dataGridView1.SelectedCells)
        dgvCell.Value = string.Empty;

private void copyToolStripMenuItem_Click(object sender, EventArgs e)

private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
    //Perform paste Operation

Copy/Paste routines follow...

The clipboard values are saved in a Dictionary, so the user can validate or manipulate the values before assigning them into gridviewcell.

private void CopyToClipboard()
    //Copy to clipboard
    DataObject dataObj = dataGridView1.GetClipboardContent();
    if (dataObj != null)

private void PasteClipboardValue()
    //Show Error if no cell is selected
    if (dataGridView1.SelectedCells.Count == 0)
        MessageBox.Show("Please select a cell", "Paste", 
		MessageBoxButtons.OK, MessageBoxIcon.Warning);

    //Get the starting Cell
    DataGridViewCell startCell = GetStartCell(dataGridView1);
    //Get the clipboard value in a dictionary
    Dictionary<int, Dictionary<int, string>> cbValue = 

    int iRowIndex = startCell.RowIndex;
    foreach (int rowKey in cbValue.Keys)
        int iColIndex = startCell.ColumnIndex;
        foreach (int cellKey in cbValue[rowKey].Keys)
            //Check if the index is within the limit
            if (iColIndex <= dataGridView1.Columns.Count - 1
            && iRowIndex <= dataGridView1.Rows.Count - 1)
                DataGridViewCell cell = dataGridView1[iColIndex, iRowIndex];

                //Copy to selected cells if 'chkPasteToSelectedCells' is checked
                if ((chkPasteToSelectedCells.Checked && cell.Selected) ||
                    cell.Value = cbValue[rowKey][cellKey];

private DataGridViewCell GetStartCell(DataGridView dgView)
    //get the smallest row,column index
    if (dgView.SelectedCells.Count == 0)
        return null;

    int rowIndex = dgView.Rows.Count - 1;
    int colIndex = dgView.Columns.Count - 1;

    foreach (DataGridViewCell dgvCell in dgView.SelectedCells)
        if (dgvCell.RowIndex < rowIndex)
            rowIndex = dgvCell.RowIndex;
        if (dgvCell.ColumnIndex < colIndex)
            colIndex = dgvCell.ColumnIndex;

    return dgView[colIndex, rowIndex];

private Dictionary<int, Dictionary<int, string>> ClipBoardValues(string clipboardValue)
    Dictionary<int, Dictionary<int, string>>
    copyValues = new Dictionary<int, Dictionary<int, string>>();

    String[] lines = clipboardValue.Split('\n');

    for (int i = 0; i <= lines.Length - 1; i++)
        copyValues[i] = new Dictionary<int, string>();
        String[] lineContent = lines[i].Split('\t');

        //if an empty cell value copied, then set the dictionary with an empty string
        //else Set value to dictionary
        if (lineContent.Length == 0)
            copyValues[i][0] = string.Empty;
            for (int j = 0; j <= lineContent.Length - 1; j++)
                copyValues[i][j] = lineContent[j];
    return copyValues;


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Balu Sathish
Software Developer Calpine Technologies
India India
No Biography provided

Comments and Discussions

QuestionchkPasteToSelectedCells Pinmembern shiva Ram24-Sep-14 3:02 
QuestionExcellent thanks. Used this with derived DataGridView class. PinmemberMember 1062780723-Jul-14 6:02 
QuestionIs it possible to Force Single Cell Only PinmemberMember 171591218-Feb-14 2:32 
GeneralThanks PinmemberMikles Max29-Apr-13 15:45 
Questionconvert to Pinmembermr.fugio29-Nov-12 20:57 
AnswerRe: convert to PinmemberMichael Paschal25-Mar-15 15:25 
Question...? PinmemberMember 926187310-Aug-12 0:49 
AnswerRe: ...? Pinmemberrndtech15-Jul-13 14:20 
GeneralGreat Code PinmemberHolyHannah21-Jun-12 4:27 
GeneralMy vote of 5 Pinmembermanoj kumar choubey5-Apr-12 8:53 
QuestionCopy and Paste PinmemberAnthony Fonseca5-Jul-11 17:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150414.5 | Last Updated 8 Jun 2011
Article Copyright 2011 by Balu Sathish
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid