如何为Telerik RadGrid中的列标题赋予颜色

时间:2012-01-24 14:33:51

标签: telerik telerik-grid radgrid radgridview

我在asp .net中创建了一个telerik RadGrid。我的要求是将自己的颜色赋予Column标题。我们怎样才能做到这一点......?下面是我正在使用的代码结构。

  <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>

它应该如下图所示: enter image description here

4 个答案:

答案 0 :(得分:4)

Telerik自动为其网格使用外观。如果您尚未指定外观,则它将使用Grid.Default外观。您可以将此外观或任何外观修改为您自己的自定义样式。只需按照以下步骤操作即可。

  • 更改css文件中的属性。 (默认的网格外观称为Grid.Default.css。在我的计算机上,它位于我在文件夹Skins \ Default中具有用于ASP.NET AJAX的telerik RadControls的文件夹中。)在这种情况下,更改css之类的东西这样的:

Grid.Default.css(第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'); */
}
  • 在您拥有网格(或母版页)的页面上添加您修改的css文件的链接。 <link href="Grid.Default.css" rel="stylesheet" type="text/css" />

  • 将属性EnableEmbeddedSkins =“false”添加到RadGrid。 <telerik:RadGrid ID="RadGrid1" EnableEmbeddedSkins="false" runat="server">这将告诉页面使用您修改的css文件而不是嵌入的文件。

Telerik的网站上有一篇名为How To Override Styles in a RadControl for ASP.NET AJAX' Embedded Skin的博文,详细解释了如何覆盖现有的风格。

答案 1 :(得分:1)

实现结果的非常方便的方法是使用......

<ItemStyle BackColor="Red"/>

<AlternatingItemStyle BackColor="Red"/>

以下是插入标签的代码......

  <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>

答案 2 :(得分:1)

我有同样的问题,甚至在重写课程后,标题颜色也没有改变。

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

我发现我的被覆盖的背景被Grid重新覆盖了。我使用!important关键字不允许Grid重新覆盖我的类

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

你可以为任何课程做同样的事情!

答案 3 :(得分:0)

我们再见面!:

步骤0 :(通过编辑添加)导航到以下安装文件夹并将网格文件夹添加到项目中

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

enter image description here


我设法制作的结果

enter image description here

第1步:创建一个样式表,我将其命名为Grid.Default.css,并将其链接到头标记之间的.aspx页面中。

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

第2步:打开所述样式表并逐字粘贴:

/*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;
}

第3步:转到您的网格并将您的css类设置为默认值并关闭嵌入式外观:

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

第4步:运行它。 (如果你想调整我使用的颜色,我试图让它尽可能接近截图中的颜色,请参见下文。)

注意:要更改颜色,请在css类中修改此声明:

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