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

Changing fore and background color of headers at runtime

15 Answers 442 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mark
Top achievements
Rank 1
Mark asked on 22 Dec 2010, 04:23 PM
In c# I would like to programmatically set the foreground and background colors of the header cells at runtime.  Is this possible?

15 Answers, 1 is accepted

Sort by
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 22 Dec 2010, 05:12 PM
Hi Mark,

Yes, you can do this. you need to subscribe to the ViewCellformatting event which fires for all cells, not just data cells. Have a look at this documentation and scroll to the View Cell formatting section near the bottom to see exmaples.

Hope that helps but let me know if you need more inofrmation
Richard
0
Mark
Top achievements
Rank 1
answered on 22 Dec 2010, 08:53 PM
Thank you for your help.  Using the ViewCellFormat event I am able to have this work:
e.CellElement.DrawBorder = false;

But my code like this has no effect:

e.CellElement.ForeColor = Color.Silver;
e.CellElement.BackColor = Color.Black;

0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 23 Dec 2010, 10:55 AM
Hi Mark,

This should work for you, but let me know if you need anyhting else
private void radGridView1_ViewCellFormatting(object sender, CellFormattingEventArgs e)
{
    if (e.CellElement is GridHeaderCellElement)
    {
        e.CellElement.ForeColor = Color.Silver;
        e.CellElement.BackColor = Color.Black;
        e.CellElement.GradientStyle = GradientStyles.Solid;
        e.CellElement.DrawBorder = false;
        e.CellElement.DrawFill = true;
    }
    else
    {
        e.CellElement.ResetValue(LightVisualElement.BackColorProperty);
        e.CellElement.ResetValue(LightVisualElement.ForeColorProperty);
        e.CellElement.ResetValue(LightVisualElement.DrawBorderProperty);
        e.CellElement.ResetValue(LightVisualElement.GradientStyleProperty);
        e.CellElement.ResetValue(LightVisualElement.DrawFillProperty);
    }
}

hope that helps
Richard
0
Mark
Top achievements
Rank 1
answered on 23 Dec 2010, 05:15 PM
First of all, thank you very much for helping me.

Secondly, that did work for me, however, now another unexpected behavior is occurring.  When I hover over a row, the border of each cell in the row is drawn, and it stays drawn until that row is updated from the bound data source.  I can include a screen shot if it will help.

Thanks again,
Mark
0
Accepted
Mark
Top achievements
Rank 1
answered on 23 Dec 2010, 06:23 PM
Nevermind, I got it with this code:

            if (e.CellElement is Telerik.WinControls.UI.GridHeaderCellElement || e.CellElement is Telerik.WinControls.UI.GridDataCellElement)
            {
                e.CellElement.ForeColor = Color.Silver;
                e.CellElement.BackColor = Color.Black;
                if (e.CellElement is Telerik.WinControls.UI.GridDataCellElement)
                {
                    e.CellElement.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
                }
                e.CellElement.DrawBorder = false;
                e.CellElement.DrawFill = true;
            }
            else
            {
                e.CellElement.ResetValue(Telerik.WinControls.UI.LightVisualElement.BackColorProperty);
                e.CellElement.ResetValue(Telerik.WinControls.UI.LightVisualElement.ForeColorProperty);
                e.CellElement.ResetValue(Telerik.WinControls.UI.LightVisualElement.DrawBorderProperty);
                e.CellElement.ResetValue(Telerik.WinControls.UI.LightVisualElement.GradientStyleProperty);
                e.CellElement.ResetValue(Telerik.WinControls.UI.LightVisualElement.DrawFillProperty);
            }

How would I changed the hover over characteristics from this event though?
0
Richard Slade
Top achievements
Rank 2
answered on 23 Dec 2010, 10:39 PM
Hi,

Im glad you have this sorted as I'm now away for the Christmas period and don't have access to a development environment. Please remember to mark all helpful posts as answer.

All the best
Richard
0
Richard Slade
Top achievements
Rank 2
answered on 24 Dec 2010, 07:17 AM
Hi, I'm glad that now works for you as I'm now away and replying from my phone. Please remember to mark helpful posts as answer. All the best. Richard
0
Mark
Top achievements
Rank 1
answered on 07 Jan 2011, 10:21 PM
Hi Richard, I am still experiencing some strange behavior from this code.

It appears that the hover over color stays with the header cells once they are hovered over the first time.  Do you know of a way to counteract this?
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 08 Jan 2011, 12:03 AM
Hi Mark,

Ok, so it looks like you need to keep that linear style on the header cells but not on the main cells.
Please can you try this and let me know how you get on.

private void radGridView1_ViewCellFormatting(object sender, CellFormattingEventArgs e)
{
    if (e.CellElement is Telerik.WinControls.UI.GridHeaderCellElement)
    {
        e.CellElement.ForeColor = Color.Silver;
        e.CellElement.BackColor2 = Color.Black;
        e.CellElement.BackColor = SystemColors.ControlDark;
        e.CellElement.GradientStyle = GradientStyles.Linear;
        e.CellElement.DrawBorder = true;
        e.CellElement.DrawFill = true;
        e.CellElement.NumberOfColors = 2;
    }
}

private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    if (e.CellElement is Telerik.WinControls.UI.GridDataCellElement)
    {
        e.CellElement.ForeColor = Color.Silver;
        e.CellElement.BackColor = Color.Black;
        e.CellElement.GradientStyle = GradientStyles.Solid;
        e.CellElement.DrawBorder = false;
        e.CellElement.DrawFill = true;
    }
}

Hope that helps
Richard
0
Mark
Top achievements
Rank 1
answered on 10 Jan 2011, 06:02 PM
I'm not certain that the previous post was answering what I was asking.

I'm not having any problems with the gradient styles of any cells.  The issue is that for the Header Cells, the foreground is the proper gradient, and when the mouse hovers over the header cell, the background changes to orange.  The problem is that when the mouse leaves the header area, the background remains orange and does not go back to black.
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 10 Jan 2011, 06:04 PM
Hi Mark,

Have you tried the above. The code I posted ensures that it will not turn to orange, and stay like that when you mouse over the header cells.
Let me know if that's not what you need
Richard
0
Mark
Top achievements
Rank 1
answered on 10 Jan 2011, 08:37 PM
Thank you, I modified the above code to the following and now have the expected behavior.  Thank you for your time.

        private void RadGridView_ViewCellFormatting(object sender, CellFormattingEventArgs e)
        {
            if(e.CellElement is Telerik.WinControls.UI.GridHeaderCellElement)
            {
                e.CellElement.ForeColor = Color.Silver;
                e.CellElement.BackColor2 = Color.Silver;
                e.CellElement.BackColor = Color.Black;
                e.CellElement.GradientStyle = Telerik.WinControls.GradientStyles.Linear;
                e.CellElement.DrawBorder = false;
                e.CellElement.DrawFill = true;
                e.CellElement.NumberOfColors = 2;
            }
        }

        private void RadGridView_CellFormatting(object sender, CellFormattingEventArgs e)
        {
            if(e.CellElement is Telerik.WinControls.UI.GridDataCellElement)
            {
                e.CellElement.ForeColor = Color.Silver;
                e.CellElement.BackColor = Color.Black;
                e.CellElement.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
                e.CellElement.DrawBorder = false;
                e.CellElement.DrawFill = true;
            }
        }
0
Richard Slade
Top achievements
Rank 2
answered on 10 Jan 2011, 08:46 PM
Great. Glad you have this working Mark and that I could be of help.
All the best
Richard
0
Mangesh
Top achievements
Rank 1
answered on 25 Jul 2011, 10:09 AM
Hi ,I am using Telerik Grid View to Bind real time data.Data is updating continuously after 1 second.My problem is that i want to change Header ,Cell backcolor and font at runtime.I am able to do this via subscribing viewcellformatting event but this event fires whenever data change so it is taking longer time and application is hanging with very worst performance.Is telerik grid is able to handle Real Time data with cell and row formatting at runtime.Let me know the solution if Telerik have.Otherwise i suggest all please dont use Telerik Grid.
0
Svett
Telerik team
answered on 28 Jul 2011, 11:53 AM
Hi Mangesh,

Please find the answer of this post in the forum thread regarding "Group row coloring"..

Regards,
Svett
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

Tags
GridView
Asked by
Mark
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Mark
Top achievements
Rank 1
Mangesh
Top achievements
Rank 1
Svett
Telerik team
Share this question
or