Programmatically change font to bound cell

Posted by: sstoecker on 21 January 2021, 12:38 pm EST

    • Post Options:
    • Link

    Posted 21 January 2021, 12:38 pm EST

    I am dynamically creating cells, which correctly shows the data on the report. The DataTable that is the data source has columns called Value1…Value{n-1} based on the number of columns returned. What I would like to do is color the text in the cell based on the value of another field. So if the “Result” value in the DataTable is “P”, I would like the font color to be green. Otherwise, I would like it to be red. Here is the code I have. Is there a way I can bind a method to check the value of the data bound to the cell, or some other way to evaluate the data passed to each cell during the binding process?

            for (int i = 1; i < columns.Count; i++)
            {
                textbox = new GrapeCity.ActiveReports.SectionReportModel.TextBox();
                textbox.Name = "row" + (i).ToString();
                textbox.DataField = $"Value{i}";
                textbox.Location = new PointF(1.4f + (i * 0.3f), 0.0f);
                textbox.Size = new SizeF(0.3f, 0.3f);
                
                // TODO: Color text based on results
                //if (row["Result"].ToString() == "P")
                //{
                //    textbox.ForeColor = System.Drawing.Color.Green;
                //}
                //else
                //{
                //    textbox.ForeColor = System.Drawing.Color.Red;
                //}
    
                ar.Sections[4].Controls.Add(textbox);
            }
    
  • Posted 22 January 2021, 12:00 am EST

    Hello,

    You can check the value of the TextBox in the Format event of the Section and change the color of the TextBox accordingly.

    Thanks,

    Mohit

  • Posted 22 January 2021, 3:41 pm EST

    Thank you! I wasn’t aware of the Format event, and that was the key. For future reference to anyone else, here is my Format method. Because most of my columns and rows are dynamically generated, with columns names like “Value0”, “Value1”, etc., I loop through every column in my report and if it’s a TextBox, I determine if there is a value in that particular TextBox and change the font color based on if the Text property is less than or greater than the MarginalRate property:

            private void detail_Format(object sender, System.EventArgs eArgs)
            {
                for (int i = 0; i < ColumnCount; i++)
                {
                    if (this.detail.Controls[i].GetType().Name == "TextBox")
                    {
                        GrapeCity.ActiveReports.SectionReportModel.TextBox textBox = (GrapeCity.ActiveReports.SectionReportModel.TextBox)this.detail.Controls[i];
    
                        if (!String.IsNullOrEmpty(textBox.Text))
                        {
                            textBox.ForeColor = int.Parse(textBox.Text) <= MarginalRate ? System.Drawing.Color.Green : System.Drawing.Color.Red;
                        }
                    }
                }
            }
    
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels