I use the following CellFormatting code to conditionally color rows in my datagridview.
private void SGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == SGridView.Columns["Name"].Index )
{
DataGridViewRow row = SGridView.Rows[e.RowIndex];
SBomRow BomRow = (SBomRow )row.DataBoundItem;
switch (BomRow.UsageType())
{
case (UsageType.NE):
break;
case (UsageType.SV):
e.CellStyle.BackColor = Color.OrangeRed;
break;
case (UsageType.Mix):
e.CellStyle.BackColor = Color.LightGray;
break;
default:
break;
}
}
}
When I copy paste from the datagridview to excel, coloring is lost. I know that data is added as HTML and CSV data to the clipboard when pasting, so it is probably impossible to keep coloring when pasting to excel. Is that true, or is there a way to keep colors when copy-pasting.
I was able to set the colors by using a modified version of the code found here. First I changed the CopyHtmlToClipBoard() to a public static void method and place it in an Extension class. Next, when calling the code, I set a var to capture each cells color and then modified the html.AppendFormat()statement to include the color being passed. Here is a copy of the code used:
I believe that DataGridView by default stores in the clipboard only tab delimited data without formatting
But you can write your custom Copy with Formatting function using the method, described in
http://www.tcx.be/blog/2005/copy-html-to-clipboard/
Just in case if you will want to ask a question how to handle copy event to write some custom code and put formatted HTML to clipboard, I would suggest using PreviewKeyDown event and write something like