This is a migrated thread and some comments may be shown as answers.

Forecolor formatting do not apply to cell

2 Answers 173 Views
GridView
This is a migrated thread and some comments may be shown as answers.
FMorales
Top achievements
Rank 1
FMorales asked on 05 Nov 2014, 11:00 AM
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 Answers, 1 is accepted

Sort by
0
Accepted
FMorales
Top achievements
Rank 1
answered on 06 Nov 2014, 12:52 PM
Problem solved.

I have just added:                 

cell.Style.BackColor = Color.Transparent;

Thanks
0
Stefan
Telerik team
answered on 07 Nov 2014, 01:08 PM
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.

 
Tags
GridView
Asked by
FMorales
Top achievements
Rank 1
Answers by
FMorales
Top achievements
Rank 1
Stefan
Telerik team
Share this question
or