Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Is it possible to copy datagridview with image column to the clipboard and paste it on excel. Cause I'm using copy datagridview to clipboard paste it on excel for excel exporting but the image column was not copied. Is there a way to fix this?

What I have tried:

private void copyAlltoClipboard()
    {
        //to remove the first blank column from datagridview
        dataGridView1.RowHeadersVisible = false;
        dataGridView1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
        dataGridView1.SelectAll();
        DataObject dataObj = dataGridView1.GetClipboardContent();
        if (dataObj != null)
            Clipboard.SetDataObject(dataObj);
    }
    private void button3_Click_1(object sender, EventArgs e)
    {
        copyAlltoClipboard();
        Microsoft.Office.Interop.Excel.Application xlexcel;
        Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
        Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
        object misValue = System.Reflection.Missing.Value;
        xlexcel = new Excel.Application();
        xlexcel.Visible = true;
        xlWorkBook = xlexcel.Workbooks.Add(misValue);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
        Excel.Range CR = (Excel.Range)xlWorkSheet.Cells[1, 1];
        CR.Select();
        xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
    }
Posted
Updated 13-Sep-21 22:30pm

1 solution

The documentation at DataGridView.ClipboardCopyMode Property (System.Windows.Forms) | Microsoft Docs[^] states:
Quote:
Gets or sets a value that indicates whether users can copy cell text values to the Clipboard and whether row and column header text is included.

Which makes, sense since you cannot mix text and images in the same copy operation.
 
Share this answer
 
Comments
Sarah Rozaizee 14-Sep-21 5:28am    
So there is no way a clipboard can copy both text and image is it? okay thank you. I'll use another way to export it to excel.
Richard MacCutchan 14-Sep-21 5:50am    
Unfortunately no, the clipboard will only save a single type at any one time.

That is not strictly true, but to get round this limitation would require quite a lot of code.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900