CellFormatting messes RadGridView up when sorting it

3 posts, 1 answers
  1. Danilo
    Danilo avatar
    47 posts
    Member since:
    Sep 2012

    Posted 27 Oct 2014 Link to this post

    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. Answer
    Stefan
    Admin
    Stefan avatar
    2908 posts

    Posted 28 Oct 2014 Link to this post

    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.

     
  3. Danilo
    Danilo avatar
    47 posts
    Member since:
    Sep 2012

    Posted 29 Oct 2014 in reply to Stefan Link to this post

    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
Back to Top