Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

That information is stored in the range:
http://msdn.microsoft.com/en-us/library/4zs9xy29%28v=vs.80%29.aspx[^]


And is set like this:
C#
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:
C#
Excel.Range r = (Excel.Range)m_objRange[2, 2];
System.Drawing.Color col = System.Drawing.ColorTranslator.FromOle((int) r.Interior.Color);
 
Share this answer
 
v2
Comments
Al Yambo 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 15-Sep-12 13:51pm    
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.clipboardcopymode.aspx ?
Al Yambo 29-Oct-12 13:08pm    
Hi Kenneth. Thanks but I do not think the code here copies the formatting.
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.

http://blog.tcx.be/2005/08/copying-html-fragment-to-clipboard.html[^]

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();
html.Append("<table>");
for (int i = 0; i <= dataGridView1.RowCount - 1; i++)
{
html.Append("<tr>");
for (int j = 0; j <= dataGridView1.ColumnCount - 1; j++)
{
DataGridViewCell cell = dataGridView1[j, i];
html.AppendFormat("<td bgcolor=green>{0}</td>", cell.Value);

}
html.Append("</tr>");
}
html.Append("</table>");
Test.CopyHtmlToClipBoard(html.ToString()); 
 
Share this answer
 
v2

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