set RowElement.BackColor in RowFormatting event

2 posts, 0 answers
  1. Tim R
    Tim R avatar
    133 posts
    Member since:
    Feb 2006

    Posted 10 Dec 2012 Link to this post

    I am able to change the font but not the backcolor.  The line that sets the color is indeed being executed (e.RowElement.BackColor = ....) but the color does not change.  What am I failing to do?

    void grid_RowFormatting(object sender, Telerik.WinControls.UI.RowFormattingEventArgs e)
            {
                try
                    {
                     
                        if ((grid.MasterTemplate.Columns.Contains("colARRIVE")) && (grid.MasterTemplate.Columns.Contains("colDEPART")))
                        {
     
                            if ((TimeSpan)e.RowElement.RowInfo.Cells["colARRIVE"].Value == (TimeSpan) e.RowElement.RowInfo.Cells["colDEPART"].Value)
                            {
                                e.RowElement.Font = new Font(e.RowElement.Font, FontStyle.Strikeout);
                            }
                            else
                            {
                                e.RowElement.Font = new Font(e.RowElement.Font, FontStyle.Regular);
                            }
     
                        }
     
                        if (e.RowElement.RowInfo.Cells["colSRC"].Value.ToString() == "O")
                        {
                            e.RowElement.BackColor = System.Drawing.Color.AliceBlue;
                        }
     
                }
                catch (Exception ex)
                {
     
                }
            }
  2. Anton
    Admin
    Anton avatar
    167 posts

    Posted 13 Dec 2012 Link to this post

    Hello Tim,

    Thank you for writing.

    By default, the DrawFill property is false, so you need to set it to true. In addition, you need to reset the RowElement's values because the RadGridView uses UI virtualization for its cells and rows i.e. they are reused to display different data - see this blog for more information.

    Here is the modified version of your code that will resolve your issue:
        if ((this.radGridView1.MasterTemplate.Columns.Contains("colARRIVE")) && (this.radGridView1.MasterTemplate.Columns.Contains("colDEPART")))
        {
     
            if ((TimeSpan)e.RowElement.RowInfo.Cells["colARRIVE"].Value == (TimeSpan)e.RowElement.RowInfo.Cells["colDEPART"].Value)
            {
                e.RowElement.Font = new Font(e.RowElement.Font, FontStyle.Strikeout);
            }
            else
            {
                e.RowElement.Font = new Font(e.RowElement.Font, FontStyle.Regular);
            }
     
        }
     
        if (e.RowElement.RowInfo.Cells["colSRC"].Value.ToString() == "O")
        {
            e.RowElement.DrawFill = true;
            e.RowElement.BackColor = System.Drawing.Color.AliceBlue;
        }
        else
        {
            e.RowElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
            e.RowElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
            e.RowElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
        }
    }
    catch (Exception ex)
    {
     
    }

    I hope this helps.

    All the best,
    Anton
    the Telerik team
    Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
Back to Top