Sorting on Columns that are derived/re-calculated

Hi Currently I am use a Column that shows "Cost" for each row.

Each row may have a different currency base, so I need to convert this Cost into a view-currency for an exchange rate.
So Displayed Cost = Cost * Exchange Rate

Currently, I used Template to do that:
<GridColumn Field="Cost">
            <Template>
                @{
                    if (context is Summary s)
                    {
                        if (s.Currency != viewCurrency)
                        {
                            var exchRate = MarketDataService.GetExchangeRate(s.Currency, viewCurrency);
                            <span>@((s.Cost* exchRate).ToString("N2"))</span>
                     }
                        else
                        {
                            <span>@(s.Cost.ToString("N2"))</span>
                        }
                    }
                }
            </Template>
        </GridColumn>

However, my problem is that the sort still used the original s.Cost for sorting. 
Roughly, I need the sort also to be calculated based on the ccy * exch.

What should be the right approach of doing this?
(I see alternative of either calculating (ccy * exch) as part of the datasource so that i can simply put it on a simple-column - but since my view currency can change (which is a setting), i want to avoid doing this as this means will need to recalculate the values for the entire datasource again.)

Thank you for your time in advance.

2 Replies

NW Nick Wong March 23, 2021 04:01 PM UTC

Similar problem when I want to do is to calculate "Weighting" of each row, relatively to the entire datasource.

For example, 
I have row 3rows that has a column (say "Value") with 10, 20, 30 respectively.

Upon setting the data source, i can calculate the total sum of the Value column, ie. 60.

Then I add a template column using (row.Value / TotalSum) such that it now shows 17%, 33%, 50% respectively.

However, the problem is that this column cannot be sorted, because there is no field to bind with. (Seems i can use Field=Value too and the sort will work correctly, because Value is just a relative of the Total)


JP Jeevakanth Palaniappan Syncfusion Team March 24, 2021 04:56 AM UTC

Hi Nick, 

Greetings from Syncfusion support. 

We have checked your query and we would like to inform you that the column template feature is only to customize the element/content of the column. The data operation like sorting, filtering etc.. will be handled only based on the value present in the datasource for that field. So we suggest you to provide the data as per your requirement in order to do sorting. 

Please get back to us if you have any other queries. 

Regards, 
Jeevakanth SP. 


Loader.
Up arrow icon