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

Cell formating and sorting problem

2 Answers 36 Views
GridView
This is a migrated thread and some comments may be shown as answers.
xclirion
Top achievements
Rank 1
xclirion asked on 03 May 2012, 10:48 AM
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 Answers, 1 is accepted

Sort by
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 03 May 2012, 11:02 AM
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
0
xclirion
Top achievements
Rank 1
answered on 03 May 2012, 11:55 AM
Hi Richard,

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

Michael
Tags
GridView
Asked by
xclirion
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
xclirion
Top achievements
Rank 1
Share this question
or