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

How can I change colors of Column Headers in code

6 Answers 900 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Tansel Ozkusaksiz
Top achievements
Rank 1
Tansel Ozkusaksiz asked on 31 Aug 2007, 07:37 AM
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?

6 Answers, 1 is accepted

Sort by
0
Dwight
Telerik team
answered on 31 Aug 2007, 01:15 PM
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
0
Paul
Top achievements
Rank 2
answered on 07 Mar 2012, 08:39 PM
Has this changed in the last 5 years? I'm having a hard time figuring this out in VB.Net.
0
Boryana
Telerik team
answered on 09 Mar 2012, 10:44 AM
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 >>
0
Paul
Top achievements
Rank 2
answered on 22 Mar 2012, 04:03 PM
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 :)
0
Boryana
Telerik team
answered on 27 Mar 2012, 08:21 AM
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 >>
0
Paul
Top achievements
Rank 2
answered on 27 Mar 2012, 04:14 PM
Awesome! works great. Many thanks :)
Tags
GridView
Asked by
Tansel Ozkusaksiz
Top achievements
Rank 1
Answers by
Dwight
Telerik team
Paul
Top achievements
Rank 2
Boryana
Telerik team
Share this question
or