In telerik documentation, It's say that aggregates values are store in the AggregatesValues
. They even use it in the exemple.
But I find it impossible to prove. As everying is true until proven wrong .. right?
Let me provide you a Minimal, Complete, and Verifiable example. So you could point my mistake.
<telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource" AllowPaging="True" ShowGroupPanel="True">
<MasterTableView>
<GroupByExpressions>
<telerik:GridGroupByExpression>
<SelectFields>
<telerik:GridGroupByField FieldAlias="GrpGroupID1" FieldName="GroupID" />
<telerik:GridGroupByField FieldAlias="SumCount" FieldName="Count" Aggregate="Sum" />
</SelectFields>
<GroupByFields>
<telerik:GridGroupByField FieldAlias="GrpGroupID" FieldName="GroupID" HeaderText="" />
</GroupByFields>
</telerik:GridGroupByExpression>
</GroupByExpressions>
<GroupHeaderTemplate>
<table>
<tr>
<td>eval GrpGroupID1:</td>
<td><%# Eval("GrpGroupID1") %></td>
<td> ||| </td>
<td>Bind GrpGroupID1:</td>
<td><%# ((GridGroupHeaderItem)Container).AggregatesValues["GrpGroupID1"] %></td>
</tr>
<tr>
<td>eval SumCount:</td>
<td><%# Eval("SumCount") %></td>
<td> ||| </td>
<td>Bind SumCount:</td>
<td><%# ((GridGroupHeaderItem)Container).AggregatesValues["SumCount"] %></td>
</tr>
</table>
</GroupHeaderTemplate>
<Columns>
<telerik:GridNumericColumn DataField="ID" HeaderText="ID" SortExpression="ID" UniqueName="Name_ID" />
<telerik:GridNumericColumn DataField="GroupID" HeaderText="GroupID" SortExpression="GroupID" UniqueName="Name_GroupID" />
<telerik:GridBoundColumn DataField="Name" HeaderText="Name" SortExpression="Name" UniqueName="Name_Name" />
<telerik:GridBoundColumn DataField="Text" HeaderText="Text" SortExpression="Text" UniqueName="Name_Text" />
<telerik:GridNumericColumn DataField="Count" HeaderText="Count" SortExpression="Count" UniqueName="Name_Count" Aggregate="Sum" />
</Columns>
</MasterTableView>
</telerik:RadGrid>
protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
List<TmpType> myData = new List<TmpType>();
List<string> firstNames = new List<string>() { "Angela", "Pamela", "Sandra", "Rita", "Monica", "Erica", "Tina", "Mary", "Jessica", "Loubega" };
List<string> Location = new List<string>() { "Reunion", "Paris", "Bretagne", "Madagascar", "UK", "Maurice" };
Random random = new Random();
for (int i = 0; i <= 88; i++)
{
TmpType row = new TmpType();
row.ID = i + 1;
row.GroupID = random.Next(10);
row.Count = random.Next(10);
row.Name = firstNames[random.Next(firstNames.Count)];
row.Text = Location[random.Next(Location.Count)];
myData.Add(row);
}
RadGrid1.DataSource = myData;
}
class TmpType
{
public string Name { get; set; }
public string Text { get; set; }
public int Count { get; set; }
public int GroupID { get; set; }
public int ID { get; set; }
}
Key and values of AggregatesValues in debug:
Exemple of data display:
As you can see in this exemple:
- Eval("SumCount")
can find the value
when :
- ((GridGroupHeaderItem)Container).AggregatesValues["SumCount"]
fail !
The documentation says:
the field alias name when you want to access the total aggregate of the items in the current group.
And SumCount
is my FieldAlias.
Here is a list of every thing i have try and the result.
Eval() : always almost correct, the approximate knowledge of nearly everything.
Eval("GrpGroupID1")
, Give the current value of the groupby field, OK!Eval("SumCount")
, Give the correct result of the aggregate function, OK!Eval("Count")
, Give the value of the row for this group (4), not Expected.Eval("Name_Count")
, Error because this is not a properties of anything, OK!AggregatesValues: It will be fast !
((GridGroupHeaderItem)Container).AggregatesValues["GrpGroupID1"]
, Give the current value of the groupby field, OK!
Everything else
, Return NULL
Those test have been made using a asp:Label
and not using an Label.
Eval
. But why? Why would I use an Eval
when MSDN state that I should not use it and when the Telerik documentation state that I can use the aggregates values collection.Many will be asking: "Where is the question?".
How can I get this GridGroupHeaderItem.AggregatesValues
without Eval
or Bind
?
The "Name_GroupID" is missing the Aggregate
property which would actually fill the Aggregate values collection. While Eval() has had access to the bound values, the Aggregate collection was still empty resulting in null values.
Try adding the Aggregate="Sum"
to the column.
<telerik:GridNumericColumn DataField="GroupID" HeaderText="GroupID" SortExpression="GroupID" UniqueName="Name_GroupID" Aggregate="Sum" />
Result :
Key and values of AggregatesValues in debug:
Example of data display:
BIG thumbs up for all the details you have put together! It was super nice to investigate this case!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With