Coloring rows in different colors

5 posts, 1 answers
  1. Nick Gebbett
    Nick Gebbett avatar
    13 posts
    Member since:
    May 2010

    Posted 03 Nov 2010 Link to this post

    Hello everybody!
    I have an issue to paint rows in different colors based on the DataBoundItem.Date Property.
    Should be like this:

    Date           |          Name
    01.01.2000           Order1       blue
    01.01.2000           Order2 blue
    02.01.2000           Order3 green
    05.01.2000           Order4 blue

    all orders that are in one day should be in one color (as even color), then the next day's orders (as not even color),  

    how to do this ? =)
  2. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 03 Nov 2010 Link to this post

    Hi Alexander,

    You need to handle the RowFormatting event. In this, you can set the various back colours as required.

    in the exmaple below, I have inspected a column called "Name", and if the value is Name, then I have set the back colour to blue.

    Private Sub RadGridView_RowFormatting(ByVal sender As System.Object, ByVal e As Telerik.WinControls.UI.RowFormattingEventArgs) Handles RadGridView.RowFormatting
        If e.RowElement.RowInfo.Cells("Name").Value.ToString() = "Name" Then
            e.RowElement.DrawFill = True
            e.RowElement.BackColor = Color.Blue
            e.RowElement.BackColor2 = Color.Blue
            e.RowElement.BackColor3 = Color.Blue
            e.RowElement.BackColor4 = Color.Blue
        End If
    End Sub

    hope that helps
    Richard
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Nick Gebbett
    Nick Gebbett avatar
    13 posts
    Member since:
    May 2010

    Posted 03 Nov 2010 Link to this post

    Hello Richard!
    Thanks for your reply!

    I have tried to do it this way but to paint rows in case I need I use an external DateTime variable:
    private bool EvenRow;
    private DateTime PreviousDate = DateTime.MinValue;
    private static readonly Color EvenRowColor = Color.FromArgb(246, 251, 255);      
    void BaseGridViewRowFormatting(object sender, RowFormattingEventArgs e)
            {
                if (ProceedColoringRows && baseGridView.MasterTemplate.SortDescriptors.Contains("DeliveryDate.Date"))
                {
                    if (((DeliveryNotification)e.RowElement.RowInfo.DataBoundItem).DeliveryDate.Date != PreviousDate.Date)
                    {
                        EvenRow = !EvenRow;
                        PreviousDate = ((DeliveryNotification)e.RowElement.RowInfo.DataBoundItem).DeliveryDate;
                    }
                    if (EvenRow)
                    {
                        e.RowElement.DrawFill = true;
                        e.RowElement.BackColor = EvenRowColor;
                        e.RowElement.GradientStyle = GradientStyles.Solid;
                    }
                    else
                    {
                        e.RowElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
                        e.RowElement.ResetValue(LightVisualElement.BackColor2Property, ValueResetFlags.Local);
                        e.RowElement.ResetValue(LightVisualElement.BackColor3Property, ValueResetFlags.Local);
                        e.RowElement.ResetValue(LightVisualElement.BackColor4Property, ValueResetFlags.Local);
                        e.RowElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
                    }
                }
                else
                {
                    e.RowElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
                    e.RowElement.ResetValue(LightVisualElement.BackColor2Property, ValueResetFlags.Local);
                    e.RowElement.ResetValue(LightVisualElement.BackColor3Property, ValueResetFlags.Local);
                    e.RowElement.ResetValue(LightVisualElement.BackColor4Property, ValueResetFlags.Local);
                    e.RowElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
                }
            }

    But BaseGridViewRowFormatting fires even when I mouseover the row.
    So I cannot define when rows painting is done. The method brakes my color when I mouseover the row.
    P.S. I have noticed that BaseGridView_SortChanging doesn't fires when i change ASC to DESC row sorting. Is this behavior correct?
  5. Answer
    Svett
    Admin
    Svett avatar
    728 posts

    Posted 09 Nov 2010 Link to this post

    Hello Alexander,

    Your code looks very well. You have only missed to reset the local value of the GradientStyle property:

    e.RowElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);

    If the code snippet above does not resolve your case, can you please open a support ticket with attached project where the issue occurs? The SortChanging event issue is introduced in the latest version. I am glad to inform your that we have addressed it for the upcoming Q3 2010 release.

    Greetings,
    Svett
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. Nick Gebbett
    Nick Gebbett avatar
    13 posts
    Member since:
    May 2010

    Posted 09 Nov 2010 Link to this post

    Thanks for your reply.
    This was not a bug but difficulty to realize my task.
    Finally I did what I needed
    The example below:
    private static readonly Color EvenRowColor = Color.FromArgb(246, 251, 255);
     
    /// <summary>
    /// Local variable to stop cyclic GridViewRowFormatting event
    /// </summary>
    private bool BreakRecursionGridViewRowFormatting = false;
     
    private void BaseGridViewRowFormatting(object sender, RowFormattingEventArgs e)
    {
        if (BreakRecursionGridViewRowFormatting) return;
        if (baseGridView.MasterTemplate.SortDescriptors.Contains("DeliveryDate.Date"))
        {
            BreakRecursionGridViewRowFormatting = true;
            // Take the date from the previous row entity
            var previousDate = e.RowElement.RowInfo.Index > 0
                                ? ((DeliveryNotification)baseGridView.Rows[e.RowElement.RowInfo.Index - 1].DataBoundItem).DeliveryDate
                                : DateTime.MinValue;
            // Paint current row
            PaintRow(e, ((DeliveryNotification)e.RowElement.RowInfo.DataBoundItem).DeliveryDate.Date != previousDate.Date);
            BreakRecursionGridViewRowFormatting = false;
        }
        else
        {
            ResetRowFormatting(e);
        }
    }
     
    private void PaintRow(RowFormattingEventArgs e, bool changeColor)
    {
        var previousColorWasEven = e.RowElement.RowInfo.Index > 0
            && baseGridView.Rows[e.RowElement.RowInfo.Index - 1].Tag is bool
            && (bool) baseGridView.Rows[e.RowElement.RowInfo.Index - 1].Tag;
        if ((!changeColor && previousColorWasEven) || (changeColor && !previousColorWasEven))
        {
            e.RowElement.DrawFill = true;
            e.RowElement.BackColor = EvenRowColor;
            e.RowElement.GradientStyle = GradientStyles.Solid;
            //When Tag property is setting the new BaseGridViewRowFormatting event will be fired
            baseGridView.Rows[e.RowElement.RowInfo.Index].Tag = true; // marker color even
        }
        else
        {
            ResetRowFormatting(e);
            baseGridView.Rows[e.RowElement.RowInfo.Index].Tag = false; // marker color not even
        }
    }
     
    private static void ResetRowFormatting(RowFormattingEventArgs e)
    {
        e.RowElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
        e.RowElement.ResetValue(LightVisualElement.BackColor2Property, ValueResetFlags.Local);
        e.RowElement.ResetValue(LightVisualElement.BackColor3Property, ValueResetFlags.Local);
        e.RowElement.ResetValue(LightVisualElement.BackColor4Property, ValueResetFlags.Local);
        e.RowElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
    }
     
Back to Top
UI for WinForms is Visual Studio 2017 Ready