Cell formating and sorting problem

3 posts, 1 answers
  1. Michael Topf
    Michael Topf avatar
    23 posts
    Member since:
    May 2010

    Posted 03 May 2012 Link to this post

    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
  2. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 03 May 2012 Link to this post

    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
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Michael Topf
    Michael Topf avatar
    23 posts
    Member since:
    May 2010

    Posted 03 May 2012 Link to this post

    Hi Richard,

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

    Michael
Back to Top