Alternating row color

14 posts, 0 answers
  1. Lovsten
    Lovsten avatar
    72 posts
    Member since:
    Aug 2007

    Posted 05 Sep 2007 Link to this post

    How can I set every alternating row to a different backcolor? Do I have to do that in the Theme manager or what?
  2. Jack
    Admin
    Jack avatar
    2335 posts

    Posted 05 Sep 2007 Link to this post

    Hello Lovsten,

    Yes, this can be done trough theme. To set an alternating background color for every odd row you can use the IsOdd property of GridRowElement. Follow these steps:

    1. Load the grid in Visual Style Builder
    2. Select a GridRowElement
    3. Add new condition and set it to fire when Telerik.WinControls.UI.GridRowElement.IsOdd is set to true
    4. Set some nice background color

    If you prefer to use an existing theme just subscribe to RowFormatting event. See the following sample code:

    this.radGridView1.RowFormatting += new RowFormattingEventHandler(radGridView1_RowFormatting);

    void radGridView1_RowFormatting(object sender, RowFormattingEventArgs e)
    {
        if (e.RowElement.IsOdd)
        {
            e.RowElement.DrawFill = true;
            e.RowElement.BackColor = Color.Red;
        }
        else
            e.RowElement.DrawFill = false;
    }


    I attached a sample theme with alternating colors based on RadGrid's Vista theme.

     
    All the best,
    Jack
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Lovsten
    Lovsten avatar
    72 posts
    Member since:
    Aug 2007

    Posted 06 Sep 2007 Link to this post

    The IsOdd thingy was very good. Thanks.
  5. Lovsten
    Lovsten avatar
    72 posts
    Member since:
    Aug 2007

    Posted 07 Sep 2007 Link to this post

    Hi Jack

    It's me again. Loved the IsOdd thing. Now I want to take it one step further. I want to indicate to the user which columns are readonly.
    Best way would be if sort of greyish those columns (still keeping the alternating row. That is:
    Cell ReadOnly Even: Light grey
    Cell Readonly Odd: Darker grey
    Cell Editable Even: white
    Cell Editable Odd: Alternative color

    Get it?

    If not possbile I guess a change of forecolor will do.

    Any chance of doing this in theme or code?
  6. Jack
    Admin
    Jack avatar
    2335 posts

    Posted 07 Sep 2007 Link to this post

    Hi Lovsten,

    At this, time this is not possible through theme. Use the CellFormatting event instead. See the following code:

    this.radGridView1.CellFormatting += new CellFormattingEventHandler(radGridView1_CellFormatting);

    void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
    {
        e.CellElement.DrawFill = true;
        e.CellElement.GradientStyle = GradientStyles.Solid;
        if (e.CellElement.ColumnInfo.ReadOnly)
        {
            if (e.CellElement.RowElement.IsOdd)
                e.CellElement.BackColor = Color.Gray;
            else
                e.CellElement.BackColor = Color.LightGray;
        }
        else
        {
            if (e.CellElement.RowElement.IsOdd)
                e.CellElement.BackColor = Color.Cyan;
            else
                e.CellElement.BackColor = Color.White;
        }
    }

     
    Best wishes,
    Jack
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  7. Lovsten
    Lovsten avatar
    72 posts
    Member since:
    Aug 2007

    Posted 10 Sep 2007 Link to this post

    That worked like a charm.

    The downside on using this is that it kills all the nice stuff from the theme. Using the Vista theme. When I have this code none of the MouseOver och IsSelected formatting works like the theme. I guess this code overrides the theme. Do I have to get this into the code in CellFormatting or?

    Do you know if it's planned to be able to use the ReadOnly value in the Theme?
  8. Jack
    Admin
    Jack avatar
    2335 posts

    Posted 10 Sep 2007 Link to this post

    Hi Lovsten,

    Yes, the code overrides the theme. To restore the default theme behavior you must prevent your code from applying when IsCurrent or IsMouseOver properties of GridRowElement are set to true:

    if (!e.CellElement.RowElement.IsCurrent  && !e.CellElement.RowElement.IsMouseOover)
    {
    // ...
    }

    We have plans to enable a new ReadOnly property in themes. This will happen in one of our next versions. 

     
    Sincerely yours,
    Jack
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  9. Lovsten
    Lovsten avatar
    72 posts
    Member since:
    Aug 2007

    Posted 11 Sep 2007 Link to this post

    This didn't help actually. The CellFormatting Event is never triggered at all when I have the mouse over
  10. Jack
    Admin
    Jack avatar
    2335 posts

    Posted 11 Sep 2007 Link to this post

    My mistake, Lovsten, for which I sincerely apologize. In some cases the CellFormatting event is not fired when IsMouseOver is changed and you cannot achieve the desired effect through code in this exact case.

    We will add support for the IsReadOnly property in themes in our next week's release.


    Regards,
    Jack
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  11. Lovsten
    Lovsten avatar
    72 posts
    Member since:
    Aug 2007

    Posted 20 Sep 2007 Link to this post

    I got the 2007 Q2 release now and I can see the IsReadonly in the theme manager. But I can't get it to work.

    For GridRowElement /root element/ I have these states:
    * Initial (white background)
    * IsOdd and IsReadOnly (dark grey background)
    * IsCurrent (white background)
    * IsMouseOver and !IsCurrent (light blue background)
    * IsOdd and !IsReadOnly (yellow background)
    * !IsOdd and IsReadOnly (light grey background)

    But the readonly doesn't seem to work. I get white background on even rows and yellow background on odd rows for all columns (also the readonly ones).




  12. Jack
    Admin
    Jack avatar
    2335 posts

    Posted 20 Sep 2007 Link to this post

    Hi Lovsten,

    Maybe some of your conditions override the IsReadOnly condition. This is possible when your conditions are not set to AutoUnapply or you are using animations intensively. I am not sure which condition causes this problem. Please open a support ticket and share your theme with us. We will be glad to help you fix it.

    I have attached a sample theme based on our Vista theme, which modifies cell color to Red when the cell is read only. Please note that simply setting the ReadOnly property of GridViewColumn to true does not update the visual state of the grid. You need also to call RadGridView.GridElement.Update(false).
     

    Best wishes,
    Jack
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  13. SHAHROKH
    SHAHROKH avatar
    8 posts
    Member since:
    Oct 2012

    Posted 27 Aug 2012 Link to this post

    Hi guys.
    I have a grid view and i want my odd rows have light blue color but below codes is not working.I checked below condition.In this condition, the RowElement.IsOdd is always equal false.Why this is happen?RowElement.IsOdd=true never happen in this condition.
     
    private void GridPanes_CellFormatting(object sender, Telerik.WinControls.UI.CellFormattingEventArgs e)
            {
                e.CellElement.DrawFill = true;
                e.CellElement.GradientStyle = GradientStyles.Solid;
                if (e.CellElement.RowElement.IsOdd)
                    e.CellElement.BackColor = Color.LightBlue;
            }

    I wrote below codes for my grid view:
     
     private void GridPanes_ViewCellFormatting_1(object sender, Telerik.WinControls.UI.CellFormattingEventArgs e)
            {
                
                e.CellElement.BackColor = Color.LightGoldenrodYellow;
                e.CellElement.BackColor2 = Color.LightGoldenrodYellow;
                e.CellElement.BorderColor = Color.Red;
            }

            private void GridPanes_ViewRowFormatting_1(object sender, Telerik.WinControls.UI.RowFormattingEventArgs e)
            {
                e.RowElement.BackColor = Color.LightBlue;
                e.RowElement.BackColor2 = Color.LightBlue;
            }

    Please help me.
  14. Jack
    Admin
    Jack avatar
    2335 posts

    Posted 29 Aug 2012 Link to this post

    Hi Shahrokh,

    The IsOdd property of GridRowElement contains valid value only when the EnableAlternatingRowColor property is set to true. You can use the following code to change the background color for odd rows in grid:
    this.radGridView1.EnableAlternatingRowColor = true;
    this.radGridView1.TableElement.AlternatingRowColor = Color.LightBlue;

    I hope this helps.
     
    All the best,
    Jack
    the Telerik team
    RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
  15. SHAHROKH
    SHAHROKH avatar
    8 posts
    Member since:
    Oct 2012

    Posted 31 Aug 2012 Link to this post

    Hello Jack.
    Thank you.That was helpful.
Back to Top
UI for WinForms is Visual Studio 2017 Ready