This is a migrated thread and some comments may be shown as answers.

Grid footer with multiple columns with custom aggregate

10 Answers 1469 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tom Ray
Top achievements
Rank 1
Tom Ray asked on 26 Oct 2010, 01:32 PM
I have a grid that I need to have several custom aggregate columns for the footer as it need to calculate the Average Daily Rate by calculating the sum(revenue in each row) / sum(rooms in each row)  this calculation needs to be done on different columns in the same grid and shown in the footer.   As there is very little documentation on Footer custom aggregates any help or samples would be appreciated.

10 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 26 Oct 2010, 02:25 PM
Hello Tom,

The following sample code will help you to achieve your requirement.

ASPX:
<telerik:RadGrid ID="RadGrid1" GridLines="None" runat="server"          
  OnCustomAggregate="RadGrid1_CustomAggregate" ShowFooter="true">
    <MasterTableView>
       <Columns>
         <telerik:GridBoundColumn DataField="rooms" HeaderText="rooms" UniqueName="rooms">
         </telerik:GridBoundColumn>
         <telerik:GridBoundColumn DataField="revenue" HeaderText="revenue" 
              UniqueName="revenue">
         </telerik:GridBoundColumn>
         <telerik:GridBoundColumn DataField="DailyRate" HeaderText="DailyRate"
            UniqueName="DailyRate" Aggregate="Custom">
         </telerik:GridBoundColumn>
      </Columns>
    </MasterTableView>
  </telerik:RadGrid>

C#:
protected void RadGrid1_CustomAggregate(object sender, GridCustomAggregateEventArgs e)
   {
       if (((Telerik.Web.UI.GridBoundColumn)e.Column).UniqueName == "DailyRate")
       {
 
           Double rooms = 0;
           Double revenue = 0;
           foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
           {
               rooms += Convert.ToDouble(item["rooms"].Text);
               revenue += Convert.ToDouble(item["revenue"].Text);
       
           }
           e.Result = revenue / rooms;
       }
   }

-Shinu.
0
Tom Ray
Top achievements
Rank 1
answered on 27 Oct 2010, 08:37 AM
Shinu,

Thanks for the quick the response.  This code works great with the exception I changed the "Telerik.Web.UI.GridBoundColumn" to "Telerik.Web.UI.GridColumn"  so that it works when there are more than one column type in the grid needing a custom footer.

Thanks again!
Tom
0
Tom Ray
Top achievements
Rank 1
answered on 27 Oct 2010, 09:24 AM
I now have a new issue. When i click on the edit button to do an edit in place I get the error "Input string was not in a correct format." in the custom aggregate function.  Any clue?
0
Shinu
Top achievements
Rank 2
answered on 27 Oct 2010, 11:22 AM
Hello Tom,

By using above code we cannot access the cell value if the item is in edit mode. In that case access the control in editform (TextBox) and then access the value and proceed with the above calculation. Sample code is given below.

C#:
protected void RadGrid1_CustomAggregate(object sender, GridCustomAggregateEventArgs e)
   {
       if (((Telerik.Web.UI.GridBoundColumn)e.Column).UniqueName == "DailyRate")
       {
           Double rooms = 0;
           Double revenue = 0;
           foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
           {
               if (!item.IsInEditMode)
               {
                   rooms += Convert.ToDouble(item["rooms"].Text);
                   revenue += Convert.ToDouble(item["revenue"].Text);
               }
               else
               {
                   TextBox txtroom = (TextBox)item["rooms"].Controls[0];
                   rooms += Convert.ToDouble(txtroom.Text);
                   TextBox txtrevenue = (TextBox)item["revenue"].Controls[0];
                   revenue += Convert.ToDouble(txtrevenue.Text);
               }
 
           }
           e.Result = revenue / rooms;
       }
   }


-Shinu.
0
Mac P
Top achievements
Rank 1
answered on 17 Nov 2012, 06:33 PM
Nice example. 

I have one issue here. I bind the grid to sql view using sql datasource and auto paging. The grid also has filtering at column level.

When i use Aggregate = Sum it gives me correct total by adding all paged items. But when i change it to custom and write below code it gives me total of only that page. What should be done so that it gives correct total?

The reason i wan't it custom because i am building a report that will add check amount for unique check numbers only.

protected void PaymentsGrid_CustomAggregate(object sender, GridCustomAggregateEventArgs e)
       {
           if (((Telerik.Web.UI.GridBoundColumn)e.Column).UniqueName == "CheckAmount")
           {
  // TODO: Filter duplicate check numbers
               Double checkAmount = 0;              
               foreach (GridDataItem item in PaymentsGrid.MasterTableView.Items)
               {
                   checkAmount += item["CheckAmount"].Text.Replace("$","").ToSafeDouble(0);            
 
               }
               e.Result = checkAmount;
           }          
       }
0
Marin
Telerik team
answered on 21 Nov 2012, 12:45 PM
Hi,

 It depends how you calculate the custom total value in the code-behind event. The Items collection of the grid holds only the items on the current page. So if you build an aggregate by iterating over them it will be calculated only for the values shown on the current page. The grid does not construct items for records on the other pages for performance reasons. But in the CustomAggregate event you can access directly the whole datasource and execute a corresponding query which will be executed for all the records and not just the ones in the current page.

Kind regards,
Marin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Mac P
Top achievements
Rank 1
answered on 29 Nov 2012, 06:11 PM
Thanks for your reply.

If i check grid.mastertableview.datasource it is always null. How do i get the rows returned?

I am binding to sql view using sql datasource. Also the grid has filtering enabled. So users may filter the grid and i need to recalculate the value accordingly.

Regards
0
Marin
Telerik team
answered on 04 Dec 2012, 10:08 AM
Hi,

 RadGrid does not persist a reference to the whole datasource for optimization purposes. That's why you cannot access directly the underlying data using the grid's API. Instead you can manually access the data by using for example the Select method of the SqlDataSource control which will return a DataView that can be enumerated.

I hope this helps.

Kind regards,
Marin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Pankaj
Top achievements
Rank 1
answered on 16 Jan 2013, 04:23 AM
Hi

I am using RADGridView for WPF and my requirement is to have Aggregates on a column based on the aggregates of different columns..

For eg.

        Qty        Discount    Net
 
         100          10           90
         200           20          180
         300           30          270
Total  600          60         Aggregate(600-60)

How can i achieve this. I know i can total net column to display 540...but my requirement is different altogether...is there a feature which can let me do this ...we are using MVVM and dont want to put any code in code behind.

Thanks
0
Marin
Telerik team
answered on 16 Jan 2013, 08:29 AM
Hello Pankaj,

 This is a forum or ASP.NET AJAX Controls, you can get more accurate help if you post your questions in the relevant forum for WPF controls where it will be handled appropriately.

Kind regards,
Marin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Tom Ray
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Tom Ray
Top achievements
Rank 1
Mac P
Top achievements
Rank 1
Marin
Telerik team
Pankaj
Top achievements
Rank 1
Share this question
or