Click here to Skip to main content
11,479,170 members (64,121 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# Windows Forms
How can I copy also the color of the cell in datagridview when copying and pasting rows to excel sheet. As of now I can only copy the cell values to excel.
Posted 15-Sep-12 8:17am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

That information is stored in the range:[^]

And is set like this:
rng.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);

I think you could get it directly with a solidcolor brush, or from this:
Excel.Range r = (Excel.Range)m_objRange[2, 2];
System.Drawing.Color col = System.Drawing.ColorTranslator.FromOle((int) r.Interior.Color);
Al Yambo at 15-Sep-12 13:37pm
When I copy a set of rows from the datagridview how do you include color information into the clipboard copy
Kenneth Haugland at 15-Sep-12 13:51pm ?
Al Yambo at 29-Oct-12 13:08pm
Hi Kenneth. Thanks but I do not think the code here copies the formatting.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Found the answer at this link. Thanks to the author and to the person who pointed me to it. The data table or datagridview has to be represented as a html table. The class "Test" is the key to this code.[^]

The example given by the author for copying and formatting a data table does not work although it has the right idea.

Here is code I wrote and tested for copying and formatting a datagridview. Take note that I did some formatting by coloring the cells green.

StringBuilder html = new StringBuilder();
for (int i = 0; i <= dataGridView1.RowCount - 1; i++)
for (int j = 0; j <= dataGridView1.ColumnCount - 1; j++)
DataGridViewCell cell = dataGridView1[j, i];
html.AppendFormat("<td bgcolor=green>{0}</td>", cell.Value);
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Maybe you can set color when exporting data like this:
columnFormat.FillStyle.Foreground = Spire.DataExport.XLS.CellColor.Pink
DataExport component is used
Al Yambo at 29-Oct-12 13:09pm
Thank you for your response. Will try it

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 215
1 F-ES Sitecore 195
2 OriginalGriff 185
3 Frankie-C 120
4 DamithSL 85
0 Sergey Alexandrovich Kryukov 7,890
1 OriginalGriff 7,366
2 Sascha Lefèvre 3,064
3 Maciej Los 2,491
4 Richard Deeming 2,335

Advertise | Privacy | Mobile
Web01 | 2.8.150520.1 | Last Updated 29 Oct 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100