Skip to content

Commit 2cf76fa

Browse files
github-actions[bot]KB Botdimodi
authored
Merge new-kb-grid-kb-style-filtered-columns-a758283dec3346e5970379897ec439e8-2617 into production (#2638)
* Added new kb article grid-kb-style-filtered-columns * Update knowledge-base/grid-kb-style-filtered-columns.md * Update knowledge-base/grid-kb-style-filtered-columns.md * Update knowledge-base/grid-kb-style-filtered-columns.md * Update knowledge-base/grid-kb-style-filtered-columns.md * kb(grid): Polish KB --------- Co-authored-by: KB Bot <kb-bot@telerik.com> Co-authored-by: Dimo Dimov <961014+dimodi@users.noreply.github.com>
1 parent 112a1a0 commit 2cf76fa

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
---
2+
title: Change Filtered Columns Color in Grid for Blazor
3+
description: Learn how to change the background color of filtered columns in the Telerik Grid for Blazor for better visibility using CSS.
4+
type: how-to
5+
page_title: How to Style Filtered Columns in Telerik Grid for Blazor
6+
slug: grid-kb-style-filtered-columns
7+
tags: grid, blazor, styling, filtering, css, oncellrender, onstatechanged
8+
res_type: kb
9+
ticketid: 1672604
10+
---
11+
12+
## Environment
13+
14+
<table>
15+
<tbody>
16+
<tr>
17+
<td>Product</td>
18+
<td>Grid for Blazor</td>
19+
</tr>
20+
</tbody>
21+
</table>
22+
23+
## Description
24+
25+
I want to style filtered columns in the Grid for Blazor using RGBA for better visibility. Specifically, when a filter is applied, I want the background color of column cells to change.
26+
27+
This knowledge base article answers the following questions:
28+
29+
* How to apply custom styles to filtered columns in a Blazor Grid?
30+
* How to enhance the visibility of filtered columns in Blazor Grid using CSS?
31+
32+
## Solution
33+
34+
To style filtered columns in a Telerik Grid for Blazor:
35+
36+
1. Use the [`OnCellRender`]({%slug grid-column-events%}#oncellrender) and [`OnStateChanged`]({%slug grid-state%}#onstatechanged) events.
37+
2. Apply a custom CSS class to the filtered columns when a filter is active. The CSS class will be rendered on each cell from these columns.
38+
39+
>caption Grid with styled filtered column.
40+
41+
`````CSHTML
42+
@using Telerik.DataSource
43+
44+
<TelerikGrid Data="@GridData"
45+
TItem="@Employee"
46+
FilterMode="@GridFilterMode.FilterRow"
47+
OnStateChanged="@OnGridStateChanged"
48+
Pageable="true"
49+
Sortable="true">
50+
<GridColumns>
51+
<GridColumn Field="@nameof(Employee.ID)"
52+
OnCellRender="@( (GridCellRenderEventArgs args) => OnGridCellRender(args, nameof(Employee.ID)) )"/>
53+
<GridColumn Field="@nameof(Employee.Name)"
54+
OnCellRender="@( (GridCellRenderEventArgs args) => OnGridCellRender(args, nameof(Employee.Name)) )" />
55+
<GridColumn Field="@nameof(Employee.Salary)" DisplayFormat="{0:C0}"
56+
OnCellRender="@( (GridCellRenderEventArgs args) => OnGridCellRender(args, nameof(Employee.Salary)) )" />
57+
</GridColumns>
58+
</TelerikGrid>
59+
60+
<style>
61+
.highlighted-column {
62+
background-color: rgba(0,0,0, 0.04);
63+
color: var(--kendo-color-primary);
64+
}
65+
</style>
66+
67+
@code {
68+
private List<Employee> GridData { get; set; } = new();
69+
private List<string> FilteredColumns { get; set; } = new();
70+
71+
private void OnGridCellRender(GridCellRenderEventArgs args, string field)
72+
{
73+
if (FilteredColumns.Contains(field))
74+
{
75+
args.Class = "highlighted-column";
76+
}
77+
}
78+
79+
private void OnGridStateChanged(GridStateEventArgs<Employee> args)
80+
{
81+
if (args.PropertyName == "FilterDescriptors")
82+
{
83+
FilteredColumns = new();
84+
85+
foreach (CompositeFilterDescriptor cfd in args.GridState.FilterDescriptors)
86+
{
87+
FilterDescriptor fd = (FilterDescriptor)cfd.FilterDescriptors.First();
88+
89+
FilteredColumns.Add(fd.Member);
90+
}
91+
}
92+
}
93+
94+
protected override void OnInitialized()
95+
{
96+
for (int i = 1; i <= 50; i++)
97+
{
98+
GridData.Add(new Employee()
99+
{
100+
ID = i,
101+
Name = $"Employee Name {i}",
102+
Salary = Random.Shared.Next(1000, 10000)
103+
});
104+
}
105+
}
106+
107+
public class Employee
108+
{
109+
public int ID { get; set; }
110+
public string Name { get; set; } = string.Empty;
111+
public decimal Salary { get; set; }
112+
}
113+
}
114+
`````
115+
## See Also
116+
117+
* [Grid Columns](https://docs.telerik.com/blazor-ui/components/grid/columns/bound)
118+
* [OnCellRender Event](https://docs.telerik.com/blazor-ui/components/grid/columns/events)
119+
* [OnStateChanged Event](https://docs.telerik.com/blazor-ui/components/grid/state#onstatechanged)
120+
* [Override the Theme or Apply Custom CSS Styles]({%slug themes-override%})

0 commit comments

Comments
 (0)