Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WinForms > GridView > Cell formating and sorting problem

Answered Cell formating and sorting problem

Feed from this thread
  • Michael Topf Michael Topf's avatar

    Posted on May 3, 2012 (permalink)

    Hi,

    I have a problem when formatting the background color of a row and then sorting the grid view. I have a gridview with 10 columns. Depending on a condition I color the background on some rows. See code. This works initially fine. But when I now sort the grid view more and more rows are colored. If I click a lot of times on different column headers all rows execept one have a formatted background. I'm surely forgot something to do, to make this work.

    private void RadGridView1CellFormatting(object sender, CellFormattingEventArgs e)
            {
                if (!String.IsNullOrEmpty(e.CellElement.RowInfo.Cells["Bearbeitungsfrist"].Value.ToString()))
                {
                    var bfrist = (DateTime)e.Row.Cells["Bearbeitungsfrist"].Value;
     
                    if (bfrist < DateTime.Now)
                    {
                        e.CellElement.DrawFill = true;
                        e.CellElement.ForeColor = Color.White;
                        e.CellElement.NumberOfColors = 1;
                        e.CellElement.BackColor = Color.Coral;
                    }
                }
            }

    Thanks for your help,
    Michael

    Reply

  • Posted on May 3, 2012 (permalink)

    Hello Michael, 

    Because of RadGridView's UI Virtualization model (it actually re-uses cells) you should reset the values of the properties you are setting when they don't meet your condition. 

    E.g. 
    private void RadGridView1CellFormatting(object sender, CellFormattingEventArgs e)
    {
        if (!String.IsNullOrEmpty(e.CellElement.RowInfo.Cells["Bearbeitungsfrist"].Value.ToString()))
        {
            var bfrist = (DateTime)e.Row.Cells["Bearbeitungsfrist"].Value;
     
            if (bfrist < DateTime.Now)
            {
                e.CellElement.DrawFill = true;
                e.CellElement.ForeColor = Color.White;
                e.CellElement.NumberOfColors = 1;
                e.CellElement.BackColor = Color.Coral;
            }
            else
            {
                e.CellElement.ResetValue(LightVisualElement.DrawFillProperty);
                e.CellElement.ResetValue(LightVisualElement.ForeColorProperty);
                e.CellElement.ResetValue(LightVisualElement.NumberOfColorsProperty);
                e.CellElement.ResetValue(LightVisualElement.BackColorProperty);
            }
        }
        else
        {
            e.CellElement.ResetValue(LightVisualElement.DrawFillProperty);
            e.CellElement.ResetValue(LightVisualElement.ForeColorProperty);
            e.CellElement.ResetValue(LightVisualElement.NumberOfColorsProperty);
            e.CellElement.ResetValue(LightVisualElement.BackColorProperty);
        }
    }

    You can read more about cell formatting here and if you care to read about the UI Virtualization and how the logical structure compares to the visual structure, you can read more about that here

    If this helps, please remember to mark as answer. If you need further assistance, then please let me know
    Richard

    Reply

  • Say Hello to Telerik's PivotGrid for ASP.NET AJAX, Silverlight, WPF and WinForms. Now packed with OLAP support.
  • Michael Topf Michael Topf's avatar

    Posted on May 3, 2012 (permalink)

    Hi Richard,

    thank you for your fast help. Now it works as intended.

    Michael

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WinForms > GridView > Cell formating and sorting problem
Related resources for "Cell formating and sorting problem"

[ Features | Demos | Documentation | Knowledge Base | Telerik TV | Code Library | Step-by-step Tutorial | Blogs | Self-Paced Trainer ]