How to give a color to the Column Header in Teleri

2019-02-26 06:27发布

问题:

I have created a telerik RadGrid in asp .net. My requirement is to give my own color to Column header. How can we achieve this...? Below is the code structure I'm using.

  <Telerik:RadGrid ID="RadGrid2" runat="server">
     <MasterTableView Width="100%" DataKeyNames="CustomerID" AllowMultiColumnSorting="True">

            <DetailTables>
                <telerik:GridTableView DataKeyNames="OrderID" Name="Orders" Width="100%">

                    <DetailTables>
                        <telerik:GridTableView DataKeyNames="OrderID" Name="OrderDetails" Width="100%">
                            <Columns>
                                <telerik:GridBoundColumn SortExpression="UnitPrice" HeaderText="Unit Price" HeaderButtonType="TextButton" DataField="UnitPrice">
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn SortExpression="Quantity" HeaderText="Quantity" HeaderButtonType="TextButton" DataField="Quantity">
                                </telerik:GridBoundColumn>
                            </Columns>
                        </telerik:GridTableView>
                    </DetailTables>

                    <Columns>
                        <telerik:GridBoundColumn SortExpression="OrderID" HeaderText="OrderID" HeaderButtonType="TextButton" DataField="OrderID">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn SortExpression="OrderDate" HeaderText="Date Ordered" HeaderButtonType="TextButton" DataField="OrderDate" UniqueName="OrderDate" DataFormatString="{0:D}">
                        </telerik:GridBoundColumn>
                    </Columns>

                </telerik:GridTableView>
            </DetailTables>

            <Columns>
                <telerik:GridBoundColumn SortExpression="CustomerID" HeaderText="CustomerID" HeaderButtonType="TextButton" DataField="CustomerID">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn SortExpression="ContactName" HeaderText="Contact Name" HeaderButtonType="TextButton" DataField="ContactName">
                </telerik:GridBoundColumn>
            </Columns>

        </MasterTableView>
    </telerik:RadGrid>

And it should looks like image below:

回答1:

Telerik automatically uses a skin for its grid. If you have not specified a skin it will use the Grid.Default skin. You can modify this skin or any of the skins to your own custom style. Just follow these steps.

  • Change the properties in the css file. (The default grid skin is called Grid.Default.css. On my computer it is found in the folder where I have the telerik RadControls for ASP.NET AJAX in the folder Skins\Default.) In this case, change the css something like this:

Grid.Default.css (line 59)

.RadGrid_Default .rgHeader,
.RadGrid_Default th.rgResizeCol
{
  border:0;
  border-bottom:1px solid #828282;
  background-color: Red;
  /* background:#eaeaea 0 -2300px repeat-x url('Grid/sprite.gif'); */
}
  • Add a link to the css file that you modified on the page where you have your grid (or in the master page). <link href="Grid.Default.css" rel="stylesheet" type="text/css" />

  • Add the property EnableEmbeddedSkins="false" to your RadGrid. <telerik:RadGrid ID="RadGrid1" EnableEmbeddedSkins="false" runat="server"> This will tell the page to use your modified css file instead of the embedded one.

Telerik's web site has a blog post called How To Override Styles in a RadControl for ASP.NET AJAX' Embedded Skin that explains in detail how to override an existing style.



回答2:

A VERY EASY way to achieve your results is to use...

<ItemStyle BackColor="Red"/>

And

<AlternatingItemStyle BackColor="Red"/>

Here is your code with the tags inserted...

  <Telerik:RadGrid ID="RadGrid2" runat="server">
     <MasterTableView Width="100%" DataKeyNames="CustomerID" AllowMultiColumnSorting="True">

            <ItemStyle BackColor="Red"/>
            <AlternatingItemStyle BackColor="Red"/>

            <DetailTables>
                <telerik:GridTableView DataKeyNames="OrderID" Name="Orders" Width="100%">

                    <DetailTables>
                        <telerik:GridTableView DataKeyNames="OrderID" Name="OrderDetails" Width="100%">
                            <Columns>
                                <telerik:GridBoundColumn SortExpression="UnitPrice" HeaderText="Unit Price" HeaderButtonType="TextButton" DataField="UnitPrice">
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn SortExpression="Quantity" HeaderText="Quantity" HeaderButtonType="TextButton" DataField="Quantity">
                                </telerik:GridBoundColumn>
                            </Columns>
                        </telerik:GridTableView>
                    </DetailTables>

                    <Columns>
                        <telerik:GridBoundColumn SortExpression="OrderID" HeaderText="OrderID" HeaderButtonType="TextButton" DataField="OrderID">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn SortExpression="OrderDate" HeaderText="Date Ordered" HeaderButtonType="TextButton" DataField="OrderDate" UniqueName="OrderDate" DataFormatString="{0:D}">
                        </telerik:GridBoundColumn>
                    </Columns>

                </telerik:GridTableView>
            </DetailTables>

            <Columns>
                <telerik:GridBoundColumn SortExpression="CustomerID" HeaderText="CustomerID" HeaderButtonType="TextButton" DataField="CustomerID">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn SortExpression="ContactName" HeaderText="Contact Name" HeaderButtonType="TextButton" DataField="ContactName">
                </telerik:GridBoundColumn>
            </Columns>

        </MasterTableView>
    </telerik:RadGrid>


回答3:

I had the same issue and even after overriding the class, the header color did not change.

.RadGrid_Default .rgHeader { background-color: rgb(236, 89, 91); }

I found that my overridden background was re-overridden by Grid. I used !important keyword not to allow Grid to re-override my class

.RadGrid_Default .rgHeader { background-color: rgb(236, 89, 91) !important; }

Same you can do for any class!



回答4:

We meet again!:

Step 0: (Added via edit) Navigate to the following install folder and add the grid folder to your project)

C:\Program Files\Telerik\RadControls for ASP.NET AJAX Q3 2011\Skins\Default


Results of what I managed to produce

Step 1: Create a stylesheet, I named mine Grid.Default.css and link it in your .aspx page between the head tags.

<head>
   <link href="Grid.Default.css" rel="stylesheet" type="text/css" />
</head>

Step 2: Open said style sheet and paste this verbatim:

/*Telerik RadGrid Default Skin*/

/*global*/

.RadGrid_Default
{
    border:1px solid #828282;
    background:#fff;
    color:#333;
}

.RadGrid_Default,
.RadGrid_Default .rgMasterTable,
.RadGrid_Default .rgDetailTable,
.RadGrid_Default .rgGroupPanel table,
.RadGrid_Default .rgCommandRow table,
.RadGrid_Default .rgEditForm table,
.RadGrid_Default .rgPager table,
.GridToolTip_Default
{
    font:12px/16px "segoe ui",arial,sans-serif;
}

.RadGrid_Default .rgAdd,
.RadGrid_Default .rgRefresh,
.RadGrid_Default .rgEdit,
.RadGrid_Default .rgDel,
.RadGrid_Default .rgFilter,
.RadGrid_Default .rgPagePrev,
.RadGrid_Default .rgPageNext,
.RadGrid_Default .rgPageFirst,
.RadGrid_Default .rgPageLast,
.RadGrid_Default .rgExpand,
.RadGrid_Default .rgCollapse,
.RadGrid_Default .rgSortAsc,
.RadGrid_Default .rgSortDesc,
.RadGrid_Default .rgUpdate,
.RadGrid_Default .rgCancel,
.RadGrid_Default .rgUngroup,
.RadGrid_Default .rgExpXLS,
.RadGrid_Default .rgExpDOC,
.RadGrid_Default .rgExpPDF,
.RadGrid_Default .rgExpCSV
{
    background-image:url('Grid/sprite.gif');
}

/*header*/

.RadGrid_Default .rgHeaderDiv
{
    background:#eee 0 -7550px repeat-x url('Grid/sprite.gif');
}
.rgTwoLines .rgHeaderDiv
{
    background-position:0 -7050px;
}

.RadGrid_Default .rgHeader {
    background-color: rgb(236, 89, 91);
}

.RadGrid_Default th.rgResizeCol
{
    border:0;
    border-bottom:1px solid #828282;
    background:#eaeaea 0 -2300px repeat-x url('Grid/sprite.gif');
}

.RadGrid_Default th.rgSorted
{
    background-color:#c4c4c4;
    background-position:0 -2600px;
}

.RadGrid_Default .rgHeader,
.RadGrid_Default .rgHeader a
{
    color:#333;
}

/*rows*/

.RadGrid_Default .rgRow td,
.RadGrid_Default .rgAltRow td,
.RadGrid_Default .rgEditRow td,
.RadGrid_Default .rgFooter td
{
    border-style:solid;
    border-width:0 0 1px 0;
}

.RadGrid_Default .rgRow td
{
    border-color:#fff;
}

.RadGrid_Default .rgAltRow
{
    background:#f2f2f2;
}

.RadGrid_Default .rgAltRow td
{
    border-color:#f2f2f2;
}

.RadGrid_Default .rgRow .rgSorted
{
    border-bottom-color:#f2f2f2;
    background-color:#f2f2f2;
}

.RadGrid_Default .rgAltRow .rgSorted
{
    border-bottom-color:#e6e6e6;
    background-color:#e6e6e6;
}

.RadGrid_Default .rgSelectedRow .rgSorted,
.RadGrid_Default .rgActiveRow .rgSorted,
.RadGrid_Default .rgHoveredRow .rgSorted,
.RadGrid_Default .rgEditRow .rgSorted
{
    background-color:transparent;
}

.RadGrid_Default .rgRow a,
.RadGrid_Default .rgAltRow a,
.RadGrid_Default .rgEditRow a,
.RadGrid_Default tr.rgEditRow a,
.RadGrid_Default tr.rgHoveredRow a,
.RadGrid_Default tr.rgActiveRow a,
.RadGrid_Default .rgFooter a,
.RadGrid_Default .rgEditForm a
{
    color:#000;
}

.RadGrid_Default .rgSelectedRow
{
    background:#828282 0 -3900px repeat-x url('Grid/sprite.gif');
    color:#fff;
}
*+html .RadGrid_Default .rgSelectedRow .rgSorted{background-color:#828282}
* html .RadGrid_Default .rgSelectedRow .rgSorted{background-color:#828282}

.RadGrid_Default .rgSelectedRow a
{
    color:#fff;
}

.RadGrid_Default .rgActiveRow,
.RadGrid_Default .rgHoveredRow
{
    background:#c5c5c5 0 -2900px repeat-x url('Grid/sprite.gif');
    color:#333;
}
*+html .RadGrid_Default .rgActiveRow .rgSorted,
*+html .RadGrid_Default .rgHoveredRow .rgSorted{background-color:#c5c5c5}
* html .RadGrid_Default .rgActiveRow .rgSorted,
* html .RadGrid_Default .rgHoveredRow .rgSorted{background-color:#c5c5c5}

.RadGrid_Default .rgEditRow
{
    background:#fff 0 -4900px repeat-x url('Grid/sprite.gif');
    color:#333;
}
*+html .RadGrid_Default .rgEditRow .rgSorted{background-color:#fff}
* html .RadGrid_Default .rgEditRow .rgSorted{background-color:#fff}

.RadGrid_Default .rgActiveRow td,
.RadGrid_Default .rgActiveRow td.rgSorted,
.RadGrid_Default .rgHoveredRow td,
.RadGrid_Default .rgHoveredRow td.rgSorted
{
    border-bottom-color:#9e9e9e;
}

.RadGrid_Default .rgSelectedRow td,
.RadGrid_Default .rgSelectedRow td.rgSorted
{
    border-bottom-color:#6c6c6c;
}

.RadGrid_Default .rgDrag
{
    background-image:url('Grid/rgDrag.gif');
}

/*footer*/

.RadGrid_Default .rgFooterDiv,
.RadGrid_Default .rgFooter
{
    background:#eee;
}

.RadGrid_Default .rgFooter td
{
    border-top:1px solid #828282;
    border-bottom:1px solid #fff;
}

/*status*/

.RadGrid_Default .rgPager .rgStatus
{
    border:1px solid;
    border-color:#828282 #c9c9c9 #eee #c9c9c9;
    border-left-width:0;
}

.RadGrid_Default .rgStatus div
{

}

/*pager*/

.RadGrid_Default .rgPager
{
    background:#eee;
}

.RadGrid_Default td.rgPagerCell
{
    border:1px solid;
    border-color:#828282 #eee #eee;
    border-right-width:0;
}

.RadGrid_Default .rgInfoPart
{
    color:#8a8a8a;
}

.RadGrid_Default .rgInfoPart strong
{
    color:#4c4e54;
}

.RadGrid_Default .rgPageFirst
{
    background-position:0 -550px;
}
.RadGrid_Default .rgPageFirst:hover
{
    background-position:0 -600px;
}
.RadGrid_Default .rgPagePrev
{
    background-position:0 -700px;
}
.RadGrid_Default .rgPagePrev:hover
{
    background-position:0 -750px;
}
.RadGrid_Default .rgPageNext
{
    background-position:0 -850px;
}
.RadGrid_Default .rgPageNext:hover
{
    background-position:0 -900px;
}
.RadGrid_Default .rgPageLast
{
    background-position:0 -1000px;
}
.RadGrid_Default .rgPageLast:hover
{
    background-position:0 -1050px;
}

.RadGrid_Default .rgPager .rgPagerButton
{
    border-color:#d0d0d0 #aeaeae #8b8b8b;
    background:#e8e8e8 repeat-x 0 -1550px url('Grid/sprite.gif');
    color:#000;
    font-family:"segoe ui",arial,sans-serif;
}

.RadGrid_Default .rgNumPart a:hover,
.RadGrid_Default .rgNumPart a:hover span,
.RadGrid_Default .rgNumPart a.rgCurrentPage,
.RadGrid_Default .rgNumPart a.rgCurrentPage span
{
    background:no-repeat url('Grid/sprite.gif');
}

.RadGrid_Default .rgNumPart a
{
    color:#000;
}

.RadGrid_Default .rgNumPart a:hover
{
    background-position:100% -1250px;
}

.RadGrid_Default .rgNumPart a:hover span
{
    background-position:0 -1150px;
}

.RadGrid_Default .rgNumPart a.rgCurrentPage,
.RadGrid_Default .rgNumPart a.rgCurrentPage:hover
{
    background-position:100% -1450px;
}

.RadGrid_Default .rgNumPart a.rgCurrentPage span,
.RadGrid_Default .rgNumPart a.rgCurrentPage:hover span
{
    background-position:0 -1350px;
}

/*sorting, reordering*/

.RadGrid_Default .rgHeader .rgSortAsc
{
    background-position:3px -248px;
    height:10px;
}

.RadGrid_Default .rgHeader .rgSortDesc
{
    background-position:3px -198px;
    height:10px;
}

.GridReorderTop_Default,
.GridReorderBottom_Default
{
    background:0 0 no-repeat url('Grid/sprite.gif');
}

.GridReorderBottom_Default
{
    background-position:0 -50px;
}

/*filtering*/

.RadGrid_Default .rgFilterRow
{
    background:#eee;
}

.RadGrid_Default .rgFilterRow td
{
    border-bottom:1px solid #828282;
}

.RadGrid_Default .rgFilter
{
    background-position:0 -300px;
}

.RadGrid_Default .rgFilter:hover
{
    background-position:0 -350px;
}

.RadGrid_Default .rgFilterActive,
.RadGrid_Default .rgFilterActive:hover
{
    background-position:0 -400px;
}

.RadGrid_Default .rgFilterBox
{
    border-color:#8e8e8e #c9c9c9 #c9c9c9 #8e8e8e;
    font-family:"segoe ui",arial,sans-serif;
    color:#333;
}

/*filter context menu*/

.RadMenu_Default .rgHCMClear,
.RadMenu_Default .rgHCMFilter
{
    border-color:#d0d0d0 #a8a8a8 #8b8b8b;

    color:#000;
    font-family:"segoe ui",arial,sans-serif;
}

.RadMenu_Default .rgHCMClear:hover,
.RadMenu_Default .rgHCMFilter:hover
{
    border-color:#d1d1d1 #b9b9b9 #a3a3a3;
    background-position:center -67px;
    background-color:#e5e5e5;
}

/*context menu*/

.GridContextMenu_Default .rmLeftImage
{

}

.GridContextMenu_Default .rgHCMSortAsc .rmLeftImage
{
    background-position:0 0;
}

.GridContextMenu_Default .rgHCMSortDesc .rmLeftImage
{
    background-position:0 -40px;
}

.GridContextMenu_Default .rgHCMUnsort .rmLeftImage
{
    background-position:0 -80px;
}

.GridContextMenu_Default .rgHCMGroup .rmLeftImage
{
    background-position:0 -120px;
}

.GridContextMenu_Default .rgHCMUngroup .rmLeftImage
{
    background-position:0 -160px;
}

.GridContextMenu_Default .rgHCMCols .rmLeftImage
{
    background-position:0 -200px;
}

.GridContextMenu_Default .rgHCMFilter .rmLeftImage
{
    background-position:0 -240px;
}

.GridContextMenu_Default .rgHCMUnfilter .rmLeftImage
{
    background-position:0 -280px;
}

/*grouping*/

.RadGrid_Default .rgGroupPanel
{
    border:0;
    border-bottom:1px solid #828282;
    background:#eee 0 -1900px repeat-x url('Grid/sprite.gif');
}

.RadGrid_Default .rgGroupPanel td
{
    border:0;
    padding:3px 4px;
}

.RadGrid_Default .rgGroupPanel td td
{
    padding:0;
}

.RadGrid_Default .rgGroupPanel .rgSortAsc
{
    background-position:4px -144px;
}

.RadGrid_Default .rgGroupPanel .rgSortDesc
{
    background-position:4px -94px;
}

.RadGrid_Default .rgUngroup
{
    background-position:0 -6998px;
}

.RadGrid_Default .rgGroupItem
{
    border:1px solid;
    border-color:#c4c4c4 #c4c4c4 #9e9e9e;
    background:#c6c6c6 0 -6500px repeat-x url('Grid/sprite.gif');
}

.RadGrid_Default .rgMasterTable td.rgGroupCol,
.RadGrid_Default .rgMasterTable td.rgExpandCol
{
    border-color:#d9d9d9;
    background:#d9d9d9 none;
}

.RadGrid_Default .rgGroupHeader
{
    background:#d9d9d9;
    font-size:1.1em;
    line-height:21px;
    color:#000;
}

.RadGrid_Default .rgGroupHeader td
{
    border-top:1px solid #828282;
    border-bottom:1px solid #d9d9d9;
}

.RadGrid_Default .rgGroupHeader td.rgGroupCol
{
    border-top-color:#828282;
}

.RadGrid_Default .rgExpand
{
    background-position:5px -496px;
}

.RadGrid_Default .rgCollapse
{
    background-position:3px -444px;
}

/*editing*/

.RadGrid_Default .rgEditForm
{
    border-bottom:1px solid #828282;
}

.RadGrid_Default .rgUpdate
{
    background-position:0 -1800px;
}

.RadGrid_Default .rgCancel
{
    background-position:0 -1850px;
}

/*hierarchy*/

.RadGrid_Default .rgDetailTable
{
    border-color:#828282;
}

/*command row*/

.RadGrid_Default .rgCommandRow
{
    background:#c5c5c5 0 -2099px repeat-x url('Grid/sprite.gif');
    color:#000;
}

.RadGrid_Default .rgCommandCell
{
    border:1px solid;
    border-color:#999 #f2f2f2;
    border-top-width:0;
    padding:0;
}

.RadGrid_Default tfoot .rgCommandCell,
.RadGrid_Default .rgMasterTable>tbody>tr.rgCommandRow .rgCommandCell
{
    border-top-width:1px;
    border-bottom-width:0;
}

.RadGrid_Default .rgCommandTable td
{
    border:0;
    padding:2px 7px;
}

.RadGrid_Default .rgCommandTable
{
    border:0;
    border-top:1px solid #fdfdfd;
    border-bottom:1px solid #e7e7e7;
}

.RadGrid_Default .rgCommandRow a
{
    color:#000;
    text-decoration:none;
}

.RadGrid_Default .rgAdd
{
    margin-right:3px;
    background-position:0 -1650px;
}

.RadGrid_Default .rgRefresh
{
    margin-right:3px;
    background-position:0 -1600px;
}

.RadGrid_Default .rgEdit
{
    background-position:0 -1700px;
}

.RadGrid_Default .rgDel
{
    background-position:0 -1750px;
}

.RadGrid_Default .rgSelected .rgDel
{
    background-position:0 -1775px;
}

.RadGrid_Default .rgExpXLS,
.RadGrid_Default .rgExpDOC,
.RadGrid_Default .rgExpPDF,
.RadGrid_Default .rgExpCSV
{

}

.RadGrid_Default .rgExpXLS
{
    background-position:0 0;
}
.RadGrid_Default .rgExpDOC
{
    background-position:0 -50px;
}
.RadGrid_Default .rgExpPDF
{
    background-position:0 -100px;
}
.RadGrid_Default .rgExpCSV
{
    background-position:0 -150px;
}

/*multirow select*/

.GridRowSelector_Default
{
    background:#4c4e54;
}

/*row drag n drop*/

.GridItemDropIndicator_Default
{
    border-top:1px dashed #666;
}

/*tooltip*/

.GridToolTip_Default
{
    border:1px solid #828282;
    padding:3px;
    background:#fff;
    color:#333;
}

/*rtl*/

.RadGridRTL_Default .rgPageFirst
{
    background-position:0 -1000px;
}
.RadGridRTL_Default .rgPageFirst:hover
{
    background-position:0 -1050px;
}
.RadGridRTL_Default .rgPagePrev
{
    background-position:0 -850px;
}
.RadGridRTL_Default .rgPagePrev:hover
{
    background-position:0 -900px;
}
.RadGridRTL_Default .rgPageNext
{
    background-position:0 -700px;
}
.RadGridRTL_Default .rgPageNext:hover
{
    background-position:0 -750px;
}
.RadGridRTL_Default .rgPageLast
{
    background-position:0 -550px;
}
.RadGridRTL_Default .rgPageLast:hover
{
    background-position:0 -600px;
}

.RadGridRTL_Default .rgExpand
{
    background-position:-20px -496px;
}

Step 3: Go to your grid and set your css class to default and turn off embedded skins:

<telerik:RadGrid ID="RadGrid1" runat="server" oninit="RadGrid1_Init" 
        onneeddatasource="RadGrid1_NeedDataSource" CssClass="Default" 
        EnableEmbeddedSkins="False">

Step 4: Run it. (If you wish to adjust the color I used, as I tried to get it as close as possible to the one in your screenshot, see below.)

NOTE: To change the color modify this declaration in the css class:

.RadGrid_Default .rgHeader 
{
   background-color: rgb(236, 89, 91);
}