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

How can get a sum aggregation for a Telerik RadGrid GridClientSelectColumn for only selected rows?

8 Answers 1114 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Brandon
Top achievements
Rank 1
Brandon asked on 23 May 2012, 03:38 PM
I'm new to the Telerik library and one of my first tasks is to get a total of the values in a numeric column of a Telerik RadGrid. The column is a GridClientSelectColumn. I understand that by setting the Aggregate="Sum", the footer will read the summation of that column. The problem is that I only want a summation of the selected rows. Can someone provide a demonstration. Thank you in advance! 

8 Answers, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 24 May 2012, 04:03 PM
Hello Brandon,

Generally, the Aggregate Value is calculated using all the items in RadGrid. Therefore, if you want to implement your specific aggregate condition, you will have to use Custom Aggregate as demonstrated for the last column in this demo:
RadGrid Footer Aggregates

For your convenience, I have created a simple RadGrid page where I implemented the desired functionality. Please give it a look and try to replace the used datasource with your own.

Greetings,
Eyup
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
Paul
Top achievements
Rank 1
answered on 20 Dec 2012, 04:31 PM
Thanks for the code. I am confused on parts of it.
 
in the code behind you have
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
   {
       foreach (GridDataItem dataItem in RadGrid1.SelectedItems)
       {
           dataItem.Selected = false;
       }
   }
 but it appears that this code is never called, so i am unclear what its purpose is, or if i should tell the grid to use that method of the OnItemDataBound event


On the .aspx page you have

<ClientSettings EnablePostBackOnRowClick="true">
    <Selecting AllowRowSelect="True" EnableDragToSelectRows="false" />
    <ClientEvents OnRowCreated="RadGrid1_RowCreated" OnRowSelected="RadGrid1_RowSelected"
                OnRowDeselected="RadGrid1_RowDeselected" OnCommand="RadGrid1_OnCommand" />
</ClientSettings>

so when i click on a row's select column, it does a postback and ends up not being selected at all......is this what is intended?

Thanks for any clarification you can add.

Paul
0
Eyup
Telerik team
answered on 24 Dec 2012, 12:17 PM
Hi Paul,

I have attached an updated, faster and more efficient web site to achieve the same functionality. Please check out the application and let me know about the result.

All the best,
Eyup
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
Paul
Top achievements
Rank 1
answered on 30 Jan 2013, 03:59 AM
Eyup,
I tried using your technique and am having trouble. What i am trying to do is a bit more complex, but starting with the basics,
I want to have a RadGrid that shows a select column and a few other columns. It will be bound to an ObjectDataSource. It should allow multiple selection of rows. It should allow clientside selection of rows but also allow me to access the selected rows in server side code. Then, most importantly I want it to sum the values in one of the columns, but only for the selected rows.

I took your example and put the required java script functions in my .aspx page....matched the settings on the RadGrid....but nothing happens when i select a single row in the RadGrid. However, if i use the checkbox in the header to select all rows, it fires the javascript functions. The javascript fails to get the value, but it at least fires. 

First, can you suggest why no javascript is firing?
Second, can you suggest what is wrong with the javascript? When i Step through, it seems to fail in the updateTotals function
function updateAggregate(value) {
        var masterTable = $find("<%= RadGrid1.ClientID %>").get_masterTableView();
        var index = masterTable.getColumnByUniqueName("RequiredAmount").get_element().cellIndex;
        var cell = masterTable.get_element().getElementsByClassName("rgFooter")[0].cells[index];
 
        var result = 0;
        if (value == "") {
            var selectedItems = masterTable.get_selectedItems();
            for (var i = 0; i < selectedItems.length; i++) {
                result += parseFloat(selectedItems[i].get_cell("RequiredAmount").textContent);
            }
        }
        else {
            result = parseFloat(cell.textContent);
            result += parseFloat(value);
        }
        cell.textContent = Math.round(result * 100) / 100;
    }

specifically, this line:
var cell = masterTable.get_element().getElementsByClassName("rgFooter")[0].cells[index];

seems to get the wrong thing....when i look at the resulting value it is "td"......then later we get to this line:
result = parseFloat(cell.textContent);
it gets NaN....

 In case it helps, here is the actual code copied from my .aspx page.  There is nothing relevant in the code behind page

<telerik:RadCodeBlock ID="RadCodeBlock" runat="server">
  
 <script type="text/javascript">
     //Put your JavaScript code here.
     function pageLoad() {
         updateAggregate("");
     }
 
     function selectionToggle(sender, args) {
         var value = args.get_item().get_selected() ? "" : "-";
         value += args.get_item().get_cell("RequiredAmount").textContent;
         updateAggregate(value);
     }
 
     function updateAggregate(value) {
         var masterTable = $find("<%= RadGrid1.ClientID %>").get_masterTableView();
         var index = masterTable.getColumnByUniqueName("RequiredAmount").get_element().cellIndex;
         var cell = masterTable.get_element().getElementsByClassName("rgFooter")[0].cells[index];
 
         var result = 0;
         if (value == "") {
             var selectedItems = masterTable.get_selectedItems();
             for (var i = 0; i < selectedItems.length; i++) {
                 result += parseFloat(selectedItems[i].get_cell("RequiredAmount").textContent);
             }
         }
         else {
             result = parseFloat(cell.textContent);
             result += parseFloat(value);
         }
         cell.textContent = Math.round(result * 100) / 100;
     }
  
    
 
    </script>
</telerik:RadCodeBlock>
       <telerik:RadGrid  ID="RadGrid1" runat="server"  DataSourceID="dsEnrollment_IncludeDonor"
            AllowMultiRowSelection="True" AutoGenerateColumns="False"  ClientSettings-ClientEvents-OnRowSelected="selectionToggle"
             >
            <ClientSettings  EnableRowHoverStyle="true">
                <Selecting AllowRowSelect="True"  />
                <ClientEvents OnRowSelected="selectionToggle" OnRowDeselected="selectionToggle" />
            </ClientSettings>
            <MasterTableView DataSourceID="dsEnrollment_IncludeDonor"
                DataKeyNames="EnrollmentID,RecipientID"
                ClientDataKeyNames="EnrollmentID,RecipientID"
                CommandItemDisplay="None" ShowFooter="true">
                <RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
                    <HeaderStyle Width="20px"></HeaderStyle>
                </RowIndicatorColumn>
                <ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
                    <HeaderStyle Width="20px"></HeaderStyle>
                </ExpandCollapseColumn>
                <NoRecordsTemplate>
                    There are no recipients that the selected Donor has previously sponsored.
                </NoRecordsTemplate>
                <Columns>
                  <telerik:GridClientSelectColumn UniqueName="ClientSelectColumn"  />
                    <telerik:GridBoundColumn DataField="EnrollmentID" UniqueName="EnrollmentID" HeaderText="Enrollment<br/>ID"
                        DataType="System.Int32" SortExpression="EnrollmentID">
                    </telerik:GridBoundColumn>
                    <telerik:GridDropDownColumn DataField="TermID" DataSourceID="dsTerms" ListTextField="Description"
                        ListValueField="TermID" UniqueName="TermID" SortExpression="TermID" HeaderText="Term" >
                    </telerik:GridDropDownColumn>
                    <telerik:GridDropDownColumn DataSourceID="dsRecipients" ListTextField="RecipientName"
                        ListValueField="RecipientID" UniqueName="RecipientID" SortExpression="RecipientID"
                        HeaderText="Recipient" DataField="RecipientID">
                    </telerik:GridDropDownColumn>
                    <telerik:GridDropDownColumn DataSourceID="dsSchools" ListTextField="SchoolName" ListValueField="SchoolID"
                        UniqueName="SchoolID" SortExpression="SchoolID" HeaderText="School" DataField="SchoolID">
                    </telerik:GridDropDownColumn>
                    <telerik:GridNumericColumn DataField="RequiredAmount" DataType="System.Double" FilterControlAltText="RequiredAmount  column"
                    HeaderText="RequiredAmount" SortExpression="RequiredAmount" UniqueName="RequiredAmount"  >
                </telerik:GridNumericColumn>
 
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
    
        <asp:Button ID="Button1" runat="server" Text="PostBack" CausesValidation="false" />
         
 <asp:ObjectDataSource ID="dsEnrollment_IncludeDonor" runat="server" SelectMethod="FetchList_NeedFunding_IncludeDonor"
            TypeName="BenemTech.SHSR_DonorMgmt.DTO.EnrollmentDTO">
            <SelectParameters>
                <asp:Parameter DefaultValue="-1" Name="donorAccountID" Type="Int32" />
            </SelectParameters>
        </asp:ObjectDataSource>

Any idea why nothing happens when i select or deselect a single row?

Follow-up question, if i use client side selection, when i eventually do a postback, are the rows selected in such a way that i can use serverside code to determine that?  If not, how do i do this?

Thanks

Paul
0
Eyup
Telerik team
answered on 04 Feb 2013, 08:09 AM
Hi Paul,

Can you please confirm that the provided application works as expected on your side, too? Please modify the web site and open a support ticket to send it back to us. Thus, we will be able to reproduce the reported issue on our side to determine the exact cause of the problem.

Kind regards,
Eyup
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
Paul
Top achievements
Rank 1
answered on 04 Feb 2013, 12:49 PM
Eyup, thanks for the reply. The app you provided worked fine so I figured it was something subtle on my page....so I started a new page and added functionality a little at a time.....and am happy to report that it is working fine now. Not sure what the problem was but it is fixed now. Thanks again
0
Gaston
Top achievements
Rank 1
answered on 11 Dec 2014, 11:28 PM
My question is how to get the sum of the column in a text box rather in the footer of the Radgrid. I am asking this because I need it to conduct further calculation from another table.
0
Eyup
Telerik team
answered on 16 Dec 2014, 12:57 PM
Hello Gaston,

I've created a sample RadGrid web site to demonstrate how you can achieve the requested functionality. Please run the attached application and let me know if it helps you.

Regards,
Eyup
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Brandon
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Paul
Top achievements
Rank 1
Gaston
Top achievements
Rank 1
Share this question
or