Copy DataGridView to Clipboard

Following on from exporting a DataGridView to a CSV file someone asked this week if it’s possible to copy the contents to the clipboard for pasting into another application.

The code below copies the content of the passed DataGridView to the clipboard inserting the specified delimiter between each column.

The basic idea is to scan each row and write the contents of the cell to a string builder, inserting the delimiter after each column (except the last one in the row). At the end of the row append a new line to the string builder. When the end of the grid is reached copy the contents of the string builder to the clipboard.

The procedure returns either true (if the DataGridView had data in it) or false (if the DataGridView is empty).

//accepts two parameters: 
//  dgvGrid - reference to a valid DataGridView
//  colDelimiter - character(s) to insert between each column
//  true if the grid has rows
//  false if the grid has no rows
private bool copyGridToClipboard(DataGridView dgvGrid, string colDelimiter)
    //variable to hold the value stored in each cell
    string cellValue;
    //variable to hold the text extracted from the grid
    StringBuilder sb = new StringBuilder();

    //set row and column count variables
    int rowCount = dgvGrid.RowCount;
    int colCount = dgvGrid.ColumnCount;

    //check to see if the grid has some rows
    if (rowCount > 0)
        //initialise the varible to hold each grid row
        DataGridViewRow gridRow = new DataGridViewRow();

        //loop through rows
        for (int row = 0; row <= rowCount - 1; row++)
            //set gridRow to the current row
            gridRow = dgvGrid.Rows[row];
            //loop through columns
            for (int col = 0; col <= colCount - 1; col++)
                //if the gridRow cell is not null assign the contents to cellValue
                if (gridRow.Cells[col].Value != null)
                    cellValue = gridRow.Cells[col].Value.ToString();
                //otherwise set cell value to an empty string
                    cellValue = string.Empty;
                //append the cell value to the stringbuilder

                //if the column is not the last one append the column delimiter
                if(col < colCount - 1)
            //end of row columns: so append newline to the stringbuilder
        }//end of final row: so copy the entire stringbuilder to the clipboard & return "true"

        Clipboard.SetData(System.Windows.Forms.DataFormats.Text, sb.ToString());
        return true;  
    //otherwise return "false"
        return false;

Below is an example of calling the procedure (NB the calling form has a DataGridView called dgvExcel)

//call sending the DataGridView dgvExcel and a comma as the delimiter
if (copyGridToClipboard(dgvExcel, ","))
    MessageBox.Show("Data Copied to the Clipboard", "Copy - Complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
    MessageBox.Show("No Data to Copy", "Copy - Failed", MessageBoxButtons.OK, MessageBoxIcon.Information);
}            }

Be First to Comment

Leave a Reply