cell formatting does not work properly when using a foreach loop

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

    Posted 30 Mar 2015 Link to this post

    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. Answer
    Todor Vyagov
    Admin
    Todor Vyagov avatar
    31 posts

    Posted 31 Mar 2015 Link to this post

    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.

     
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Danilo
    Danilo avatar
    40 posts
    Member since:
    Sep 2012

    Posted 31 Mar 2015 in reply to Todor Vyagov Link to this post

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