Forecolor formatting do not apply to cell

3 posts, 1 answers
  1. FMorales
    FMorales avatar
    26 posts
    Member since:
    Aug 2014

    Posted 05 Nov 2014 Link to this post

    Hi,

    I am trying to apply cell color depending to the text value of a cell in a row, but I does not works.

    What I am doing wrong?

    I am following the telerik example:

    http://www.telerik.com/help/winforms/gridview-cells-formatting-cells.html

    Here is my code that I invoke after pupulate data by a datasource binding list.

    private void RefreshColorsOfResultsInGrid()
    {
        this.radGridViewControl.BeginInit();
     
        foreach (GridViewRowInfo row in this.radGridViewControl.Rows)
        {
            GridViewCellInfo cell = row.Cells["ResultAsString"];
     
            if ((cell.Value.ToString().ToUpper() == "NEW") || (cell.Value.ToString().ToUpper() == "NEW*"))
            {
                cell.Style.CustomizeFill = true;                   
                cell.Style.DrawFill = true;
                cell.Style.ForeColor = Color.Green;
                cell.Style.Font = new Font("Arial", 8, FontStyle.Bold);                   
            }
            else if ((cell.Value.ToString().ToUpper() == "OLD") || (cell.Value.ToString().ToUpper() == "OLD*") )
            {
                cell.Style.CustomizeFill = true;
                cell.Style.DrawFill = true;                   
                cell.Style.ForeColor = Color.Red;
                cell.Style.Font = new Font("Arial", 8, FontStyle.Bold);
            }
            else
            {
                cell.Style.CustomizeFill = true;
                cell.Style.DrawFill = true;
                cell.Style.ForeColor = Color.Black;
            }
        }
     
        this.radGridViewControl.EndInit();
        this.radGridViewControl.Refresh();
     
    }
  2. Answer
    FMorales
    FMorales avatar
    26 posts
    Member since:
    Aug 2014

    Posted 06 Nov 2014 in reply to FMorales Link to this post

    Problem solved.

    I have just added:                 

    cell.Style.BackColor = Color.Transparent;

    Thanks
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 07 Nov 2014 Link to this post

    Hi Francisco,

    Thank you for writing.

    That is correct, if you need to set the back color, you should use the BackColor property. Here is how your method might look like:
     Font font = new Font("Arial", 8, FontStyle.Bold);
     private void RefreshColorsOfResultsInGrid()
     {
         foreach (GridViewRowInfo row in this.radGridView1.Rows)
         {
             GridViewCellInfo cell = row.Cells["ResultAsString"];
             cell.Style.CustomizeFill = true;
             cell.Style.DrawFill = true;
             cell.Style.Font = font;
     
             if ((cell.Value.ToString().ToUpper() == "NEW") || (cell.Value.ToString().ToUpper() == "NEW*"))
             {
                 cell.Style.BackColor = Color.Green;
                 cell.Style.Font = font;
             }
             else if ((cell.Value.ToString().ToUpper() == "OLD") || (cell.Value.ToString().ToUpper() == "OLD*"))
             {
                 cell.Style.BackColor = Color.Red;
                 cell.Style.Font = font;
             }
             else
             {
                 cell.Style.BackColor = Color.Black;
             }
         }
     }

    Should you have any other questions or suggestions, do not hesitate to contact us.

    Regards,
    Stefan
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top