Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
785
Setting Background Color During Excel Export
posted

Hello All,

I am exporting a XamDataGrid to Excel. A lot of the cells have a boolean value in them. For these cells, rather than exporting the strings "TRUE" and "FALSE", I want to leave the cell blank if the value is false, and I want to color it yellow if the value is true. Below is my attempt to do this. The cells with a value of true are being colored black, even though I explicitly stated "Yellow". How can I fix this?

Thanks,
Dave

private void HandleCellExporting(object sender, CellExportingEventArgs e)
{
   // Non-routed event
   e.Cancel = true;

   int row = e.CurrentRowIndex;
   int col = e.CurrentColumnIndex;

   string cellContent = string.Empty;

   if (e.Value is string)
      cellContent = (string)e.Value;
   else if (e.Value is bool)
   {
      if ((bool)e.Value)
      {
         e.Workbook.Worksheets[e.CurrentWorksheet.Index].Rows[row].Cells[col].
              CellFormat.FillPattern = FillPatternStyle.Solid;

         e.Workbook.Worksheets[e.CurrentWorksheet.Index].Rows[row].Cells[col].
              CellFormat.FillPatternBackgroundColor = Color.FromName("Yellow");
      }
   }

   e.Workbook.Worksheets[e.CurrentWorksheet.Index].Rows[row].Cells[col].Value =
        cellContent;
}