|
| 1 | +--- |
| 2 | +title: Adjust Grid Column Header Styles Dynamically |
| 3 | +description: Learn how to dynamically change the style of a column header cell in a Telerik Blazor Grid based on condition. |
| 4 | +type: how-to |
| 5 | +page_title: How to Style Column Header Cells Dynamically in a Telerik Blazor Grid |
| 6 | +slug: grid-dynamically-adjusting-column-header-styles |
| 7 | +tags: grid, blazor, headerclass |
| 8 | +res_type: kb |
| 9 | +ticketid: 1670074 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | + <tbody> |
| 16 | + <tr> |
| 17 | + <td>Product</td> |
| 18 | + <td>Grid for Blazor, <br /> TreeList for Blazor</td> |
| 19 | + </tr> |
| 20 | + </tbody> |
| 21 | +</table> |
| 22 | + |
| 23 | +## Description |
| 24 | + |
| 25 | +I am dynamically creating Grid columns in a loop and trying to adjust the column header cell style based on some condition and value from the [column field]({%slug components/grid/columns/bound%}#data-binding). Ideally, I want to implement logic to make this adjustment based on the value in the header cell. I have not been able to achieve this in the `HeaderTemplate` tag or in any of the cell render events handlers. In other templates, I have access to the `@context` field, which would make this easy, but that doesn't seem to work in the `HeaderTemplate`. |
| 26 | + |
| 27 | +## Solution |
| 28 | + |
| 29 | +To style a column header cell when dynamically creating columns in a loop, use the [`HeaderClass` parameter]({%slug components/grid/columns/bound%}#appearance) to set a CSS class under a condition and apply different styles depending on the class. For scenarios with numerous and more complex conditions, you can create a method to determine the appropriate class. |
| 30 | + |
| 31 | +Note that the [`HeaderTemplate`]({%slug grid-templates-column-header%}) does not receive a context argument because it is not related to rows and models, as outlined in the [Templates Overview]({%slug components/grid/features/templates%}) of the Telerik UI for Blazor documentation. |
| 32 | + |
| 33 | +### Example |
| 34 | + |
| 35 | +````CSHTML |
| 36 | +<TelerikGrid Data="@GridData"> |
| 37 | + <GridColumns> |
| 38 | + <GridColumn Field="@nameof(Product.Name)" Title="Product Name" /> |
| 39 | + @for (int i = 0; i <= MaxYears; i++) |
| 40 | + { |
| 41 | + <GridColumn @key="@i" Field="@( $"StartYear{i}" )" Title=@( $"{StartYear + i}" ) DisplayFormat="{0:N}" HeaderClass="@GetHeaderClass(StartYear + i)" /> |
| 42 | + } |
| 43 | + </GridColumns> |
| 44 | +</TelerikGrid> |
| 45 | +
|
| 46 | +<style> |
| 47 | + .past-year-column { |
| 48 | + background-color: yellow; |
| 49 | + } |
| 50 | +
|
| 51 | + .previous-year-column { |
| 52 | + background-color: royalblue; |
| 53 | + } |
| 54 | +
|
| 55 | + .current-year-column { |
| 56 | + background-color: pink; |
| 57 | + } |
| 58 | +
|
| 59 | + .future-year-column { |
| 60 | + background-color: red; |
| 61 | + } |
| 62 | +</style> |
| 63 | +
|
| 64 | +
|
| 65 | +@code { |
| 66 | + private List<Product> GridData { get; set; } = new(); |
| 67 | + private const int MaxYears = 10; |
| 68 | + private const int StartYear = 2020; |
| 69 | + private int currYear { get; set; } = DateTime.Now.Year; |
| 70 | +
|
| 71 | + private string GetHeaderClass(int year) |
| 72 | + { |
| 73 | + if (year <= StartYear) |
| 74 | + return "past-year-column"; |
| 75 | + else if (year < currYear) |
| 76 | + return "previous-year-column"; |
| 77 | + else if (year == currYear) |
| 78 | + return "current-year-column"; |
| 79 | + else |
| 80 | + return "future-year-column"; |
| 81 | + } |
| 82 | +
|
| 83 | + protected override void OnInitialized() |
| 84 | + { |
| 85 | + GridData = new List<Product>(); |
| 86 | +
|
| 87 | + for (int i = 1; i <= 20; i++) |
| 88 | + { |
| 89 | + GridData.Add(new Product |
| 90 | + { |
| 91 | + Id = i, |
| 92 | + Name = "Product name " + i, |
| 93 | + StartYear0 = Random.Shared.Next(100, 9999), |
| 94 | + StartYear1 = Random.Shared.Next(100, 9999), |
| 95 | + StartYear2 = Random.Shared.Next(100, 9999), |
| 96 | + StartYear3 = Random.Shared.Next(100, 9999), |
| 97 | + StartYear4 = Random.Shared.Next(100, 9999), |
| 98 | + StartYear5 = Random.Shared.Next(100, 9999), |
| 99 | + StartYear6 = Random.Shared.Next(100, 9999), |
| 100 | + StartYear7 = Random.Shared.Next(100, 9999), |
| 101 | + StartYear8 = Random.Shared.Next(100, 9999), |
| 102 | + StartYear9 = Random.Shared.Next(100, 9999), |
| 103 | + StartYear10 = Random.Shared.Next(100, 9999) |
| 104 | + }); |
| 105 | +
|
| 106 | + } |
| 107 | +
|
| 108 | + base.OnInitialized(); |
| 109 | + } |
| 110 | +
|
| 111 | + public class Product |
| 112 | + { |
| 113 | + public int Id { get; set; } |
| 114 | + public string Name { get; set; } |
| 115 | + public double? StartYear0 { get; set; } |
| 116 | + public double? StartYear1 { get; set; } |
| 117 | + public double? StartYear2 { get; set; } |
| 118 | + public double? StartYear3 { get; set; } |
| 119 | + public double? StartYear4 { get; set; } |
| 120 | + public double? StartYear5 { get; set; } |
| 121 | + public double? StartYear6 { get; set; } |
| 122 | + public double? StartYear7 { get; set; } |
| 123 | + public double? StartYear8 { get; set; } |
| 124 | + public double? StartYear9 { get; set; } |
| 125 | + public double? StartYear10 { get; set; } |
| 126 | + } |
| 127 | +} |
| 128 | +``` |
| 129 | +
|
| 130 | +## See Also |
| 131 | +
|
| 132 | +- [Grid HeaderClass Parameter]({%slug components/grid/columns/bound%}#appearance) |
| 133 | +- [Grid Column Header Template]({%slug grid-templates-column-header%}) |
| 134 | +- [Templates Overview - Telerik UI for Blazor]({%slug components/grid/features/templates%}) |
0 commit comments