|
| 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 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, filtered columns, 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 | +- How to apply custom styles to filtered columns in a Blazor Grid? |
| 29 | +- How to enhance the visibility of filtered columns in Blazor Grid using CSS? |
| 30 | + |
| 31 | +## Solution |
| 32 | + |
| 33 | +To style filtered columns in a Telerik Grid for Blazor: |
| 34 | +1. Utilize the [`OnCellRender`]({%slug grid-column-events%}#oncellrender) and [`OnStateChanged`]({%slug grid-state%}#onstatechanged) events |
| 35 | +2. Apply a custom CSS class to the filtered column when a filter is active |
| 36 | + |
| 37 | +>caption Grid with styled filtered column. |
| 38 | +
|
| 39 | +`````CSHTML |
| 40 | +@using Telerik.DataSource.Extensions |
| 41 | +@using Telerik.DataSource |
| 42 | +
|
| 43 | +<style> |
| 44 | + .highlighted-column { |
| 45 | + background-color: rgba(255, 0, 0, 0.2); /* Change to your desired RGBA */ |
| 46 | + color: #191970; |
| 47 | + } |
| 48 | +</style> |
| 49 | +
|
| 50 | +<TelerikGrid TItem="@Employee" |
| 51 | + OnRead="@ReadItems" |
| 52 | + FilterMode="@GridFilterMode.FilterRow" |
| 53 | + Sortable="true" |
| 54 | + Pageable="true" |
| 55 | + OnStateChanged="@OnGridStateChanged"> |
| 56 | + <GridColumns> |
| 57 | + <GridColumn Field="@nameof(Employee.ID)" Filterable="false" Title="ID" /> |
| 58 | + <GridColumn Field="@nameof(Employee.Name)" OnCellRender="@OnNameColumnCellRender" Title="Name" /> |
| 59 | + <GridColumn Field="@nameof(Employee.HireDate)" OnCellRender="@OnHireDateColumnCellRender" Title="Hire Date" /> |
| 60 | + </GridColumns> |
| 61 | +</TelerikGrid> |
| 62 | +
|
| 63 | +@code { |
| 64 | + private List<Employee> SourceData { get; set; } |
| 65 | + private string FilteredColumn { get; set; } |
| 66 | +
|
| 67 | + private void OnNameColumnCellRender(GridCellRenderEventArgs args) |
| 68 | + { |
| 69 | + if (FilteredColumn == nameof(Employee.Name)) |
| 70 | + { |
| 71 | + args.Class = "highlighted-column"; // Apply the custom CSS class |
| 72 | + } |
| 73 | + } |
| 74 | +
|
| 75 | + private void OnHireDateColumnCellRender(GridCellRenderEventArgs args) |
| 76 | + { |
| 77 | + if (FilteredColumn == nameof(Employee.HireDate)) |
| 78 | + { |
| 79 | + args.Class = "highlighted-column"; // Apply the custom CSS class |
| 80 | + } |
| 81 | + } |
| 82 | +
|
| 83 | + private async Task OnGridStateChanged(GridStateEventArgs<Employee> args) |
| 84 | + { |
| 85 | + if (args.PropertyName == "FilterDescriptors") |
| 86 | + { |
| 87 | + if (args.GridState.FilterDescriptors.Any()) |
| 88 | + { |
| 89 | + var filterDescriptors = new List<IFilterDescriptor>(args.GridState.FilterDescriptors); |
| 90 | + var filterDescriptor = filterDescriptors[0] as CompositeFilterDescriptor; |
| 91 | + var filteredColumn = filterDescriptor.FilterDescriptors[0] as FilterDescriptor; |
| 92 | +
|
| 93 | + // Capture the filtered column's field name |
| 94 | + FilteredColumn = filteredColumn.Member; |
| 95 | + } |
| 96 | + else |
| 97 | + { |
| 98 | + FilteredColumn = null; // Reset when no filters are applied |
| 99 | + } |
| 100 | +
|
| 101 | + StateHasChanged(); // Re-render the grid |
| 102 | + } |
| 103 | + } |
| 104 | +
|
| 105 | + protected async Task ReadItems(GridReadEventArgs args) |
| 106 | + { |
| 107 | + // Simulate network delay |
| 108 | + await Task.Delay(100); |
| 109 | +
|
| 110 | + var datasourceResult = SourceData.ToDataSourceResult(args.Request); |
| 111 | +
|
| 112 | + args.Data = datasourceResult.Data; |
| 113 | + args.Total = datasourceResult.Total; |
| 114 | + } |
| 115 | +
|
| 116 | + protected override void OnInitialized() |
| 117 | + { |
| 118 | + SourceData = GenerateData(); |
| 119 | + } |
| 120 | +
|
| 121 | + private List<Employee> GenerateData() |
| 122 | + { |
| 123 | + var result = new List<Employee>(); |
| 124 | + var rand = new Random(); |
| 125 | + for (int i = 0; i < 100; i++) |
| 126 | + { |
| 127 | + result.Add(new Employee() |
| 128 | + { |
| 129 | + ID = i, |
| 130 | + Name = "Name " + i, |
| 131 | + HireDate = DateTime.Now.Date.AddDays(rand.Next(-20, 20)) |
| 132 | + }); |
| 133 | + } |
| 134 | +
|
| 135 | + return result; |
| 136 | + } |
| 137 | +
|
| 138 | + public class Employee |
| 139 | + { |
| 140 | + public int ID { get; set; } |
| 141 | + public string Name { get; set; } |
| 142 | + public DateTime HireDate { get; set; } |
| 143 | + } |
| 144 | +} |
| 145 | +````` |
| 146 | +## See Also |
| 147 | + |
| 148 | +- [Grid Columns](https://docs.telerik.com/blazor-ui/components/grid/columns/bound) |
| 149 | +- [OnCellRender Event](https://docs.telerik.com/blazor-ui/components/grid/columns/events) |
| 150 | +- [OnStateChanged Event](https://docs.telerik.com/blazor-ui/components/grid/state#onstatechanged) |
| 151 | +- [Override the Theme or Apply Custom CSS Styles]({%slug themes-override%}) |
0 commit comments