Conditional background fill colour

I have created a report using C# code and want to change the background color based on the condition of two other fields. However the code does not work. It sets all the background color to orange though the values differ.

I tried using the code from this link but it did not work https://www.fast-report.com/en/forum/index....c=14573&hl=


Is there any thing I am doing wrong?

Comments

  • edited January 2019
    TextObject decemberTarget = new TextObject
                {
                    Name = "DecemberTarget",  
                    Left = 670.95f,
                    Width = 28.35f,
                    Height = 24.57f,
                    HorzAlign = HorzAlign.Right,
                    VertAlign = VertAlign.Center,
                    Font = new Font("Arial", 8),
                    Text = "[Data.DecemberTarget]",
            BeforePrintEvent = "DecemberTarget_BeforePrint"    
                };
    
    report.ScriptText = @"using System;
    using System.Drawing;
    namespace FastReport
    {  
      public class ReportScript
      {
        private void DecemberTarget_BeforePrint(object sender, EventArgs e)
        {
          int value = (int)Report.GetColumnValue(""Data.DecemberAchieved"");
          if (value < 6)
        DecemberTarget.FillColor = Color.Orange;
          else
              DecemberTarget.FillColor = Color.Green;
        }
      }
    }
    ";
    
  • Hello!

    Inside the BeforePrint handler, always set the Fill property of the current instance (TextObject) of `sender` (not just the global object), and be sure to include both if/else branches:

    private void Cell_BeforePrint(object sender, EventArgs e)
    {
    var cell = (TextObject)sender;
    int achieved = Convert.ToInt32(Report.GetColumnValue(“Data.DecemberAchieved”));
    int target = Convert.ToInt32(Report.GetColumnValue(“Data.DecemberTarget”));
    cell.Fill = (achieved < target)
    ? new SolidFill(Color.Orange)
    : new SolidFill(Color.LightGreen);
    }
    
This discussion has been closed.