How can I change colors of Column Headers in code

7 posts, 0 answers
  1. Tansel Ozkusaksiz
    Tansel Ozkusaksiz avatar
    1 posts
    Member since:
    Feb 2007

    Posted 31 Aug 2007 Link to this post

    Hi,
    I need to change the column header color of grid in code.
    (Changing Telerik Vista Green Header to blue header for example) I did it by using custom Theme XML, however I need to change in code also. Is there a way to do it programmatically?
  2. Dwight
    Admin
    Dwight avatar
    475 posts

    Posted 31 Aug 2007 Link to this post

    Hi Tansel,

    Here is a sample code that changes the background color of the header cell in the column named "ColumnName":
    this.radGridView1.MasterGridViewInfo.TableHeaderRow.Cells["ColumnName"].CellElement.BackColor = Color.FromArgb(100, 0, 255, 0); 

    Have in mind, that once you change a property, it is no longer controlled by the ThemeManager, i.e. the color will stay whatever you do.

    When you want to return the control to the ThemeManager, use:
    this.radGridView1.MasterGridViewInfo.TableHeaderRow.Cells["Column Name"].CellElement.ResetValue(LightVisualElement.BackColorProperty); 

    If you have further problems controlling elements' appearance, drop us a line and we'll get back to you.

     
    Regads,
    Evtim
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. Paul
    Paul avatar
    5 posts
    Member since:
    Feb 2012

    Posted 07 Mar 2012 Link to this post

    Has this changed in the last 5 years? I'm having a hard time figuring this out in VB.Net.
  4. Boryana
    Admin
    Boryana avatar
    330 posts

    Posted 09 Mar 2012 Link to this post

    Hello Paul,

    Thank you for writing.

    Currently, the best approach to set the backcolor of the header row is to subscribe to the ViewRowFormatting event of RadGridView and check whether the RowElement is of type GridTableHeaderRowElement. Here is a sample snippet:
    Private Sub radGridView1_ViewRowFormatting(sender As Object, e As RowFormattingEventArgs)
        If TypeOf e.RowElement Is GridTableHeaderRowElement Then
            e.RowElement.BackColor = Color.Red
        End If
    End Sub

    If you would like to reset the set value, use the following line:
    e.RowElement.ResetValue(LightVisualElement.BackColorProperty, Telerik.WinControls.ValueResetFlags.Local)

    Feel free to write back if you have further questions on the topic.

    Regards,
    Boryana
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
  5. Paul
    Paul avatar
    5 posts
    Member since:
    Feb 2012

    Posted 22 Mar 2012 Link to this post

    Thanks for the response.
    Is there a way to change color based on the headers text value?
    Say you have one column with header text 'Mon' and one with header text 'Tue', can you (in code) change color based on header text value? I've not been able to figure it out.

    Thanks :)
  6. Boryana
    Admin
    Boryana avatar
    330 posts

    Posted 27 Mar 2012 Link to this post

    Hello Paul,

    Thank you for writing back.

    If you would like to assign different color to each header cell depending on its text you should use the ViewCellFormatting event. Here is an example:
    Private Sub RadGridView1_ViewCellFormatting(sender As System.Object, e As Telerik.WinControls.UI.CellFormattingEventArgs) Handles RadGridView1.ViewCellFormatting
            If TypeOf e.CellElement Is GridHeaderCellElement Then
                e.CellElement.DrawBorder = True
                e.CellElement.DrawFill = True
                e.CellElement.GradientStyle = Telerik.WinControls.GradientStyles.Solid
                Select Case e.CellElement.Text
                    Case "EmployeeID"
                        e.CellElement.BackColor = Color.Yellow
                        Exit Select
                    Case "LastName"
                        e.CellElement.BackColor = Color.Orange
                        Exit Select
                    Case "FirstName"
                        e.CellElement.BackColor = Color.Red
                        Exit Select
                    Case "Title"
                        e.CellElement.BackColor = Color.Green
                        Exit Select
                    Case "TitleOfCourtesy"
                        e.CellElement.BackColor = Color.LightGreen
                        Exit Select
                    Case "City"
                        e.CellElement.BackColor = Color.LightBlue
                        Exit Select
                    Case "Country"
                        e.CellElement.BackColor = Color.Purple
                        Exit Select
                End Select
            End If
        End Sub

    For implementation details please refer to the attached project.

    I hope this helps. Let me know if you have further queries.

    Kind regards,
    Boryana
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
  7. Paul
    Paul avatar
    5 posts
    Member since:
    Feb 2012

    Posted 27 Mar 2012 Link to this post

    Awesome! works great. Many thanks :)
Back to Top