Skip to content

Commit 0b93921

Browse files
github-actions[bot]KB Bot
andauthored
Added new kb article grid-yellow-background-column-header (#599)
Co-authored-by: KB Bot <[email protected]>
1 parent 69abdbe commit 0b93921

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
---
2+
title: Applying a Yellow Background to a Column and Its Header in RadGrid
3+
description: Learn how to set a yellow background color for both the header and data cells of a specific column in RadGrid for ASP.NET AJAX.
4+
type: how-to
5+
page_title: How to Set a Yellow Background for RadGrid Column and Header
6+
slug: grid-yellow-background-column-header
7+
tags: radgrid, asp.net ajax, background color, column, header
8+
res_type: kb
9+
ticketid: 1665545
10+
---
11+
12+
## Environment
13+
14+
<table>
15+
<tbody>
16+
<tr>
17+
<td>Product</td>
18+
<td>RadGrid for ASP.NET AJAX</td>
19+
</tr>
20+
<tr>
21+
<td>Version</td>
22+
<td>all</td>
23+
</tr>
24+
</tbody>
25+
</table>
26+
27+
## Description
28+
29+
I want to apply a yellow background color to both the header and the data cells of a specific column in the RadGrid. How can I achieve this?
30+
31+
This KB article also answers the following questions:
32+
- How can I change the background color of a RadGrid column?
33+
- How to apply CSS styles to RadGrid columns?
34+
- How to use ItemStyle and HeaderStyle properties in RadGrid?
35+
36+
## Solution
37+
38+
To color the "Freight" column and its header with a yellow background in RadGrid, use a combination of CSS and the `ItemStyle`/`HeaderStyle` properties. Follow the steps below to implement the solution:
39+
40+
1. Define a CSS class for the yellow background and apply it to the "Freight" column header through code-behind during the `ItemCreated` event.
41+
2. Directly set the background color for the data cells using the `ItemStyle` and `HeaderStyle` properties in the ASPX markup.
42+
43+
### ASPX
44+
45+
```aspx
46+
<style>
47+
.yellow-column {
48+
background-color: yellow !important; /* Applies the yellow background */
49+
background-image: none !important; /* Removes Telerik's default background-image from the column header cell */
50+
}
51+
</style>
52+
53+
<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" Width="800px" OnNeedDataSource="RadGrid1_NeedDataSource" OnItemCreated="RadGrid1_ItemCreated">
54+
<MasterTableView AutoGenerateColumns="False" DataKeyNames="OrderID">
55+
<Columns>
56+
<telerik:GridBoundColumn DataField="OrderID" DataType="System.Int32"
57+
FilterControlAltText="Filter OrderID column" HeaderText="OrderID"
58+
ReadOnly="True" SortExpression="OrderID" UniqueName="OrderID">
59+
</telerik:GridBoundColumn>
60+
<telerik:GridDateTimeColumn DataField="OrderDate" DataType="System.DateTime"
61+
FilterControlAltText="Filter OrderDate column" HeaderText="OrderDate"
62+
SortExpression="OrderDate" UniqueName="OrderDate">
63+
</telerik:GridDateTimeColumn>
64+
<telerik:GridNumericColumn DataField="Freight" DataType="System.Decimal"
65+
FilterControlAltText="Filter Freight column" HeaderText="Freight"
66+
SortExpression="Freight" UniqueName="Freight">
67+
<ItemStyle BackColor="Yellow" />
68+
<HeaderStyle BackColor="Yellow" />
69+
</telerik:GridNumericColumn>
70+
<telerik:GridBoundColumn DataField="ShipName"
71+
FilterControlAltText="Filter ShipName column" HeaderText="ShipName"
72+
SortExpression="ShipName" UniqueName="ShipName">
73+
</telerik:GridBoundColumn>
74+
<telerik:GridBoundColumn DataField="ShipCountry"
75+
FilterControlAltText="Filter ShipCountry column" HeaderText="ShipCountry"
76+
SortExpression="ShipCountry" UniqueName="ShipCountry">
77+
</telerik:GridBoundColumn>
78+
</Columns>
79+
</MasterTableView>
80+
</telerik:RadGrid>
81+
```
82+
83+
### ASPX.CS
84+
85+
```csharp
86+
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
87+
{
88+
if (e.Item is GridHeaderItem)
89+
{
90+
// Color the "Freight" header cell
91+
GridHeaderItem headerItem = (GridHeaderItem)e.Item;
92+
headerItem["Freight"].CssClass = "yellow-column";
93+
94+
}
95+
}
96+
97+
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
98+
{
99+
(sender as RadGrid).DataSource = OrdersTable();
100+
}
101+
102+
private DataTable OrdersTable()
103+
{
104+
DataTable dt = new DataTable();
105+
106+
dt.Columns.Add(new DataColumn("OrderID", typeof(int)));
107+
dt.Columns.Add(new DataColumn("OrderDate", typeof(DateTime)));
108+
dt.Columns.Add(new DataColumn("Freight", typeof(decimal)));
109+
dt.Columns.Add(new DataColumn("ShipName", typeof(string)));
110+
dt.Columns.Add(new DataColumn("ShipCountry", typeof(string)));
111+
112+
dt.PrimaryKey = new DataColumn[] { dt.Columns["OrderID"] };
113+
114+
for (int i = 0; i < 70; i++)
115+
{
116+
int index = i + 1;
117+
118+
DataRow row = dt.NewRow();
119+
120+
row["OrderID"] = index;
121+
row["OrderDate"] = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0).AddHours(index);
122+
row["Freight"] = index * 0.1 + index * 0.01;
123+
row["ShipName"] = "Name " + index;
124+
row["ShipCountry"] = "Country " + index;
125+
126+
dt.Rows.Add(row);
127+
}
128+
129+
return dt;
130+
}
131+
```
132+
133+
### More Details
134+
135+
- **ItemStyle and HeaderStyle**: These properties allow you to apply the background color directly through declarative means for both the header and the data cells in the "Freight" column.
136+
- **CSS Solution**: The `.yellow-column` class is applied in the `ItemCreated` event to ensure the yellow background is enforced, while also removing any default background-image applied by Telerik.
137+
138+
By following these steps, the "Freight" column in the RadGrid will have a yellow background for both the header and data cells.
139+
140+
## See Also
141+
142+
- [Change the background color of a RadGrid column](https://docs.telerik.com/devtools/aspnet-ajax/knowledge-base/grid-change-column-background-color)

0 commit comments

Comments
 (0)