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

set RowElement.BackColor in RowFormatting event

1 Answer 111 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Tim R
Top achievements
Rank 1
Tim R asked on 10 Dec 2012, 07:38 PM
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)
            {
 
            }
        }

1 Answer, 1 is accepted

Sort by
0
Anton
Telerik team
answered on 13 Dec 2012, 05:02 PM
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.
Tags
GridView
Asked by
Tim R
Top achievements
Rank 1
Answers by
Anton
Telerik team
Share this question
or