@@ -64,7 +64,11 @@ To enable aggregates:
6464> caption Use Aggregates in the Telerik Blazor Grid
6565
6666```` CSHTML
67- <TelerikGrid Data=@GridData Groupable="true" Height="700px">
67+ @using Telerik.DataSource
68+
69+ <TelerikGrid Data=@GridData
70+ Groupable="true"
71+ OnStateInit="@( (GridStateEventArgs<Employee> args) => OnGridStateInit(args) )">
6872 <GridAggregates>
6973 <GridAggregate Field=@nameof(Employee.Name) Aggregate="@GridAggregateType.Count" />
7074 <GridAggregate Field=@nameof(Employee.Team) Aggregate="@GridAggregateType.Count" />
@@ -76,33 +80,36 @@ To enable aggregates:
7680 <GridColumns>
7781 <GridColumn Field=@nameof(Employee.Name) Groupable="false">
7882 <FooterTemplate>
79- Total: @context.Count employees
83+ Total employees : @context.Count
8084 <br />
8185 @{
8286 // you can use aggregates for other fields/columns by extracting the desired one by its
8387 // field name and aggregate function from the AggregateResults collection
8488 // The type of its Value is determined by the type of its field - decimal for the Salary field here
85- decimal salaries = (decimal)context.AggregateResults
89+ decimal? salaries = (decimal? )context.AggregateResults
8690 .FirstOrDefault(r => r.AggregateMethodName == nameof(GridAggregateType.Sum) && r.Member == nameof(Employee.Salary))?.Value;
91+
92+ <span>Total salaries: @salaries?.ToString("C0")</span>
8793 }
88- Total salaries: @salaries.ToString("C0")
8994 </FooterTemplate>
9095 </GridColumn>
9196 <GridColumn Field=@nameof(Employee.Team) Title="Team">
9297 <GroupHeaderTemplate>
93- @context.Value @* the default text you would get without the template *@
94- <span>Team size: @context.Count</span>
98+ <span>
99+ @context.Value @* the default text you would get without the template *@
100+ with employee count: @context.Count
101+ </span>
95102 </GroupHeaderTemplate>
96103 <GroupFooterTemplate>
97104 Team Members: <strong>@context.Count</strong>
98105 </GroupFooterTemplate>
99106 </GridColumn>
100- <GridColumn Field=@nameof(Employee.Salary) Title="Salary" Groupable="false">
107+ <GridColumn Field=@nameof(Employee.Salary) Title="Salary" Groupable="false" DisplayFormat="{0:C0}" >
101108 <GroupFooterTemplate>
102109 @* you can use a group footer for non-groupable columns as well *@
103- Total salaries: @context.Sum
110+ Total salaries: @context.Sum?.ToString("C0")
104111 <br />
105- <span style="color: red;">Highest: @context.Max</span>
112+ <span style="color: red;">Highest: @context.Max?.ToString("C0") </span>
106113 </GroupFooterTemplate>
107114 </GridColumn>
108115 <GridColumn Field=@nameof(Employee.ActiveProjects) Title="Active Projects">
@@ -119,14 +126,15 @@ To enable aggregates:
119126 </GroupHeaderTemplate>
120127 <GroupFooterTemplate>
121128 @*access the aggregates of the ActiveProjects column*@
122- All active projects: @context.Sum
129+ Active projects in team: @context.Sum
130+
131+ @* access the aggregates of the other columns if any *@
123132 <br />
124- @*access the aggregates of the other columns*@
125- Total teams: @context.AggregateResults[nameof(Employee.Team)].Count
133+ <span>Total teams: @context.AggregateResults[nameof(Employee.Team)]?.Count</span>
126134 <br />
127- Total employees: @context.AggregateResults[nameof(Employee.Name)].Count
135+ <span> Total employees: @context.AggregateResults[nameof(Employee.Name)]? .Count</span>
128136 <br />
129- Average salary: @context.AggregateResults[nameof(Employee.Salary)].Average.Value. ToString("C0")
137+ <span> Average salary: @context.AggregateResults[nameof(Employee.Salary)]? .Average?. ToString("C0")</span>
130138 </GroupFooterTemplate>
131139 </GridColumn>
132140 </GridColumns>
@@ -135,19 +143,26 @@ To enable aggregates:
135143@code {
136144 private List<Employee> GridData { get; set; } = new();
137145
146+ private void OnGridStateInit(GridStateEventArgs<Employee> args)
147+ {
148+ args.GridState.GroupDescriptors.Add(new GroupDescriptor()
149+ {
150+ Member = nameof(Employee.Team)
151+ });
152+ }
153+
138154 protected override void OnInitialized()
139155 {
140- for (int i = 0 ; i < 15 ; i++)
156+ for (int i = 1 ; i <= 5 ; i++)
141157 {
142- Random rnd = new Random();
143158 GridData.Add(new Employee()
144- {
145- EmployeeId = i,
146- Name = "Employee " + i.ToString() ,
147- Team = "Team " + i % 3 ,
148- Salary = rnd .Next(1000, 5000),
149- ActiveProjects = i % 4 == 0 ? 2 : 5
150- });
159+ {
160+ EmployeeId = i,
161+ Name = $ "Employee {i}" ,
162+ Team = $ "Team { i % 2 + 1}" ,
163+ Salary = Random.Shared .Next(1000, 5000),
164+ ActiveProjects = i % 4 == 0 ? 2 : 5
165+ });
151166 }
152167 }
153168
0 commit comments