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

Change Grid Column Header Font Color

3 Answers 975 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rob
Top achievements
Rank 1
Rob asked on 02 Nov 2011, 06:31 PM
I have a grid that has databound columns that are defined in the aspx code.  When preparing the data for NeedDataSource(), more columns will be dynamically added to the right side of the grid.  This all works great.  My problem is that some of these dynamically added columns will need the color of the font of the LinkButton (for sorting) in the header to turn gray once databound.  Which header column needs to change will be tracked on the server side.  So my question is:  Where is the best place on the server side (which event) to change the font color for the header column and how?

I've tried setting the .ForeColor property to gray & setting the .CssClass property in the ItemCreated, ItemDatabound, & PreRender events to no avail.  I do have a custom skin on my grid.

3 Answers, 1 is accepted

Sort by
0
Rob
Top achievements
Rank 1
answered on 02 Nov 2011, 08:45 PM
I definitey think I need to override the custom skin being used on the grid, because I created a simple example without a custom skin and it works.  Here is my working code for this simple example.  I would like to know how to change the header column text color for only 1 column, the middle column "456" in this example by using and overriding a customized skin.

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <telerik:RadScriptManager ID="mgrJS" runat="server">
        </telerik:RadScriptManager>
        <telerik:radajaxmanager ID="mgrAjax" runat="server">
        </telerik:radajaxmanager>       
        <telerik:RadGrid runat="server" ID="dg" Skin="WebBlue" AllowSorting="true" ></telerik:RadGrid>
    </div>
    </form>
</body>
</html>
Imports System.Data
  
Partial Class _Default
    Inherits System.Web.UI.Page
  
    Private Function CreateDataTable() As DataTable
        Dim dt As New DataTable
        dt.Columns.Add("123", GetType(String))
        dt.Columns.Add("456", GetType(String))
        dt.Columns.Add("789", GetType(String))
        Dim r As DataRow = dt.NewRow
        r(0) = "abc"
        r(1) = "def"
        r(2) = "ghi"
        dt.Rows.Add(r)
        Return dt
    End Function
  
    Protected Sub dg_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles dg.ItemDataBound
        If e.Item.ItemType = GridItemType.Header Then
            Dim hdrItem As GridHeaderItem = CType(e.Item, GridHeaderItem)
            For i As Integer = 0 To hdrItem.Cells.Count - 1
                If hdrItem.Cells(i).Controls.Count > 0 Then
                    Dim ctrl As LinkButton = CType(hdrItem.Cells(i).Controls(0), LinkButton)
                    If ctrl IsNot Nothing Then
                        Dim val As Integer = 0
                        Integer.TryParse(ctrl.Text, val)
                        If val = 456 Then
                            ctrl.ForeColor = Drawing.Color.Gray 
                        End If
                    End If
                End If
            Next
        End If
    End Sub
  
    Protected Sub dg_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles dg.NeedDataSource
        dg.DataSource = CreateDataTable()
    End Sub
End Class
0
Jayesh Goyani
Top achievements
Rank 2
answered on 03 Nov 2011, 11:17 AM
Hello,

<Columns>
                    <telerik:GridBoundColumn DataField="ID1" HeaderText="ID1" UniqueName="ID1">
                    </telerik:GridBoundColumn>
                     <telerik:GridBoundColumn DataField="ID2" HeaderText="ID2" UniqueName="ID2">
                    <HeaderStyle CssClass="customHeader" />
                    </telerik:GridBoundColumn>
                
                </Columns>

<style type="text/css">
       .customHeader
       {
           color : Red !important;
       }
   </style>

see attached image for result.


Thanks,
Jayesh Goyani
0
Rob
Top achievements
Rank 1
answered on 03 Nov 2011, 05:32 PM

Jayesh,

You definitely pointed me in the right direction.  Your solution works with Telerik's "canned" skins, but still not with a customized version of a Telerik skin.  Here is my final solution.  I added some styles to the aspx and set the CssClass in the code behind.

<style type="text/css">
        a.HeaderItem:link,
        a.HeaderItem:visited {
          color:Gray !important;
        }
  
        a.HeaderItem:hover {
          color:Gray !important;
        }
  
        a.HeaderItem:active {
          color:Gray !important;
        
</style>

Protected Sub dg_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles dg.ItemDataBound 
        If e.Item.ItemType = GridItemType.Header Then
            Dim hdrItem As GridHeaderItem = CType(e.Item, GridHeaderItem) 
            For i As Integer = 0 To hdrItem.Cells.Count - 1 
                If hdrItem.Cells(i).Controls.Count > 0 Then
                    Dim ctrl As LinkButton = CType(hdrItem.Cells(i).Controls(0), LinkButton) 
                    If ctrl IsNot Nothing Then
                        Dim val As Integer = 0 
                        Integer.TryParse(ctrl.Text, val) 
                        If val = 456 Then
                            ctrl.CssClass = "HeaderItem"
                        End If
                    End If
                End If
            Next
        End If
    End Sub
Tags
Grid
Asked by
Rob
Top achievements
Rank 1
Answers by
Rob
Top achievements
Rank 1
Jayesh Goyani
Top achievements
Rank 2
Share this question
or