Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GridGroupHeaderItem.AggregatesValues without Eval

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.

Aspx :

<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>

Code behind :

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; }
}

Result :

Key and values of AggregatesValues in debug:

Key and values of AggregatesValues in debug

Exemple of data display: 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.

What I try:

Here is a list of every thing i have try and the result.

Eval() : always almost correct, the approximate knowledge of nearly everything.

  1. Eval("GrpGroupID1"), Give the current value of the groupby field, OK!
  2. Eval("SumCount"), Give the correct result of the aggregate function, OK!
  3. Eval("Count"), Give the value of the row for this group (4), not Expected.
  4. Eval("Name_Count"), Error because this is not a properties of anything, OK!

AggregatesValues: It will be fast !

  1. ((GridGroupHeaderItem)Container).AggregatesValues["GrpGroupID1"], Give the current value of the groupby field, OK!

  2. Everything else, Return NULL

Those test have been made using a asp:Label and not using an Label.

Side note:

  • Yes, I could simply use the 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.

Where is the question?

Many will be asking: "Where is the question?".
How can I get this GridGroupHeaderItem.AggregatesValues without Eval or Bind ?

like image 792
Drag and Drop Avatar asked Nov 08 '22 04:11

Drag and Drop


1 Answers

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:

enter image description here enter image description here

Example of data display:

enter image description here

BIG thumbs up for all the details you have put together! It was super nice to investigate this case!

like image 196
Attila Antal Avatar answered Nov 30 '22 06:11

Attila Antal