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

Alternating row color

13 Answers 618 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Lovsten
Top achievements
Rank 1
Lovsten asked on 05 Sep 2007, 12:24 PM
How can I set every alternating row to a different backcolor? Do I have to do that in the Theme manager or what?

13 Answers, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 05 Sep 2007, 04:43 PM
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
0
Lovsten
Top achievements
Rank 1
answered on 06 Sep 2007, 11:15 AM
The IsOdd thingy was very good. Thanks.
0
Lovsten
Top achievements
Rank 1
answered on 07 Sep 2007, 07:43 AM
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?
0
Jack
Telerik team
answered on 07 Sep 2007, 12:26 PM
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
0
Lovsten
Top achievements
Rank 1
answered on 10 Sep 2007, 09:19 AM
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?
0
Jack
Telerik team
answered on 10 Sep 2007, 05:32 PM
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
0
Lovsten
Top achievements
Rank 1
answered on 11 Sep 2007, 05:58 AM
This didn't help actually. The CellFormatting Event is never triggered at all when I have the mouse over
0
Jack
Telerik team
answered on 11 Sep 2007, 03:20 PM
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
0
Lovsten
Top achievements
Rank 1
answered on 20 Sep 2007, 07:29 AM
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).




0
Jack
Telerik team
answered on 20 Sep 2007, 03:07 PM
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
0
SHAHROKH
Top achievements
Rank 2
answered on 27 Aug 2012, 04:32 PM
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.
0
Jack
Telerik team
answered on 29 Aug 2012, 09:53 AM
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 >>
0
SHAHROKH
Top achievements
Rank 2
answered on 31 Aug 2012, 06:04 AM
Hello Jack.
Thank you.That was helpful.
Tags
GridView
Asked by
Lovsten
Top achievements
Rank 1
Answers by
Jack
Telerik team
Lovsten
Top achievements
Rank 1
SHAHROKH
Top achievements
Rank 2
Share this question
or