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

cell formatting does not work properly when using a foreach loop

2 Answers 256 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Danilo
Top achievements
Rank 1
Danilo asked on 30 Mar 2015, 08:02 AM
Hey all

I'm trying to add a background color to a RadGridView cell. I often did it before and it always works perfectly, just in this case it's not working properly. Here you can see my code:

if (e.Column.Name == "BM")
{
    foreach (string rs in BmList)
    {
        if (rs == e.Row.Cells["RS"].Value.ToString())
        {
            e.CellElement.DrawFill = true;
            e.CellElement.BackColor = Color.FromArgb(238, 127, 0);
            e.CellElement.NumberOfColors = 1;
        }

        else
        {
            e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
            e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
            e.CellElement.ResetValue(LightVisualElement.NumberOfColorsProperty, ValueResetFlags.Local);
        }
    }
}

This code is just coloring 1 cell, even if there are more in the list. The debugger says, in my list called "BmList" are 18 items. But it colors just one. So I'm wondering why its not coloring every cell. When I remove the foreach loop, it works fine. And when I use this code instead of "e.CellElement" it works as well:

e.Row.Cells["BM"].Style.BackColor = Color.FromArgb(238, 127, 0);
e.Row.Cells["BM"].Style.DrawFill = true;
e.Row.Cells["BM"].Style.CustomizeFill = true;

Does anyone know about that behaviour? Or am I doing something wrong?

I hope someone can help me

Regards,
Danilo

2 Answers, 1 is accepted

Sort by
0
Accepted
Todor
Telerik team
answered on 31 Mar 2015, 08:39 AM
Hello Danillo,

Thank you for writing.

The code snippet you posted will have always have at most one cell with colored background. For each cell in the CellFormatting event you are iterating through the list of strings called BmList and only if the last string in the list equals the cell value the BackColor is changed, otherwise formatting is reset. You can use List<T>.Contains method instead.
if (e.Column.Name == "BM")
{
    if (e.Row.Cells["RS"].Value != null)
    {
        string cellValue = e.Row.Cells["RS"].Value.ToString();
        if (BmList.Contains(cellValue))
        {
            e.CellElement.DrawFill = true;
            e.CellElement.BackColor = Color.FromArgb(238, 127, 0);
            e.CellElement.NumberOfColors = 1;
        }
        else
        {
            e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
            e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
            e.CellElement.ResetValue(LightVisualElement.NumberOfColorsProperty, ValueResetFlags.Local);
        }
    }
}
else
{
    e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
    e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
    e.CellElement.ResetValue(LightVisualElement.NumberOfColorsProperty, ValueResetFlags.Local);
}

More information about formatting cells can be found here.

I hope you find this information useful.

Regards,
Todor Vyagov
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
0
Danilo
Top achievements
Rank 1
answered on 31 Mar 2015, 09:16 AM
Hello Todor Vyagov

Wasn't thinking about to try it in this way. Sounds quite logical. Thank you very much, it's working fine.

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