Skip to content

Commit 2cdb492

Browse files
committed
docs(Grid): Add example and update after review
1 parent 7d55f3a commit 2cdb492

File tree

1 file changed

+98
-3
lines changed

1 file changed

+98
-3
lines changed

knowledge-base/grid-dynamically-adjusting-column-header-styles.md

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,109 @@ I am dynamically creating Grid columns in a loop and trying to adjust the column
2626

2727
## Solution
2828

29-
To dynamically add style to a column's header cell when dynamically creating columns in a loop, use the [HeaderClass parameter]({%slug components/grid/columns/bound%}#appearance) to set a class under a condition and apply different styles depending on the class. For scenarios with numerous and more complex conditions, create a method to determine the appropriate class.
29+
To dynamically add style to a column's header cell when dynamically creating columns in a loop, use the [`HeaderClass` parameter]({%slug components/grid/columns/bound%}#appearance) to set a 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.
3030

31-
It's important to 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.
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.
3232

3333
### Example
3434

35-
```csharp
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 Field="@("Y"+i)" Title=@((StartYear + i).ToString()) DisplayFormat="{0:N}" HeaderClass="@GetHeaderClass(StartYear + i)"/>
42+
}
43+
</GridColumns>
44+
</TelerikGrid>
3645
46+
<style>
47+
.very-past-year-column {
48+
background-color: yellow;
49+
}
50+
51+
.past-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; }
67+
private int MaxYears = 10;
68+
private int StartYear = 2020;
69+
70+
private string GetHeaderClass(int year)
71+
{
72+
switch (year)
73+
{
74+
case <= 2020:
75+
return "very-past-year-column";
76+
case < 2024:
77+
return "past-year-column";
78+
case 2024:
79+
return "current-year-column";
80+
default:
81+
return "future-year-column";
82+
}
83+
}
84+
85+
protected override void OnInitialized()
86+
{
87+
GridData = new List<Product>();
88+
89+
Random rnd = new Random();
90+
91+
for (int i = 1; i <= 30; i++)
92+
{
93+
GridData.Add(new Product
94+
{
95+
Id = i,
96+
Name = "Product name " + i,
97+
Y0 = rnd.Next(100, 9999),
98+
Y1 = rnd.Next(100, 9999),
99+
Y2 = rnd.Next(100, 9999),
100+
Y3 = rnd.Next(100, 9999),
101+
Y4 = rnd.Next(100, 9999),
102+
Y5 = rnd.Next(100, 9999),
103+
Y6 = rnd.Next(100, 9999),
104+
Y7 = rnd.Next(100, 9999),
105+
Y8 = rnd.Next(100, 9999),
106+
Y9 = rnd.Next(100, 9999),
107+
Y10 = rnd.Next(100, 9999)
108+
});
109+
110+
}
111+
112+
base.OnInitialized();
113+
}
114+
115+
public class Product
116+
{
117+
public int Id { get; set; }
118+
public string Name { get; set; }
119+
public double? Y0 { get; set; }
120+
public double? Y1 { get; set; }
121+
public double? Y2 { get; set; }
122+
public double? Y3 { get; set; }
123+
public double? Y4 { get; set; }
124+
public double? Y5 { get; set; }
125+
public double? Y6 { get; set; }
126+
public double? Y7 { get; set; }
127+
public double? Y8 { get; set; }
128+
public double? Y9 { get; set; }
129+
public double? Y10 { get; set; }
130+
}
131+
}
37132
```
38133
39134
## See Also

0 commit comments

Comments
 (0)