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

CellFormatting messes RadGridView up when sorting it

2 Answers 28 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Danilo
Top achievements
Rank 1
Danilo asked on 27 Oct 2014, 03:54 PM
Hello

I'm using CellFormatting event to color some cells in my RadGridView. On Form_Load it works fine (see first image). But when I sort the RadGridView it messes the whole thing up (see second image). You can see my code here:

if (e.Column.Name == "va" && e.CellElement.Value != null)
{
    if ((e.CellElement.Value.ToString() == "P" && e.Row.Cells["id"].Value.ToString() == "1") ||
        (e.CellElement.Value.ToString() == "A" && e.Row.Cells["id"].Value.ToString() == "1") ||
        (e.CellElement.Value.ToString() == "E" && e.Row.Cells["id"].Value.ToString() == "1"))
    {
        e.CellElement.DrawFill = true;
        e.CellElement.BackColor = Color.Red;
        e.CellElement.NumberOfColors = 1;
    }

    else if (e.CellElement.Value.ToString() == "P" || e.CellElement.Value.ToString() == "A" || e.CellElement.Value.ToString() == "E")
    {
        e.CellElement.DrawFill = true;
        e.CellElement.BackColor = Color.Orange;
        e.CellElement.NumberOfColors = 1;
    }
}

Any suggestions how to fix this?

Thanks in advance

2 Answers, 1 is accepted

Sort by
0
Accepted
Stefan
Telerik team
answered on 28 Oct 2014, 11:28 AM
Hi Danilo,

Thank you for writing.

RadGridView uses UI virtualization
 for its cells, which means that there are only certain amount of cells visible at all times and the data in these cells is being changed when scrolling, hence it is necessary to reset each visual settings you apply. More information and examples you can find here: http://www.telerik.com/help/winforms/gridview-cells-formatting-cells.html.

In your case, you should add the following code at the end of your CellFormatting event handler:
else
{
    e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
    e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
    e.CellElement.ResetValue(LightVisualElement.NumberOfColorsProperty, ValueResetFlags.Local);
}

I hope that you find this information useful. Should you have any other questions, 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.

 
0
Danilo
Top achievements
Rank 1
answered on 29 Oct 2014, 09:02 AM
Hi Stefan

Thanks for the answer. I didn't know that but I'll remember next time. It works like a charm.
Thank you very much!

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