HI,
I have users that would like to sort the rows for Seller Name as in the screen shot. Is this possible?
Hello Kevin,
The OrderByKeyExpression will always sort Ascending by the element that you pass it, but it is possible to do your own custom logic by defining your own Func<Sale, <T>>. For example, using the City hierarchy, you could do something like the following to sort in a different direction:
string[] _cities = "Sofia;London;New York;Berlin;Seattle;Mellvile;Tokyo".Split(';'); List<string> citiesDescending = _cities.OrderByDescending(c => c[2]).ToList(); Func<Sale, int> func = (e) => { return citiesDescending.IndexOf(e.City); }; Expression<Func<Sale, int>> expression = city => func(city); cityHierarchy.LevelDescriptors[1].OrderByKeyExpression = expression;
The above gets all of the cities that are available in the hierarchical level, sorts them descending, and then uses the index of the city in the descending List to apply a custom sort. I hope this helps you.
Please let me know if you have any other questions or concerns on this matter.
Hi Andrew,
Thanks for all your help with this. I have one more question. Do you know how to write the following OrderByKeyExpression so that it sorts Ascending?
char[] separators = { ' ' }; Expression<Func<Sale, String>> expression2 = sale => sale.Seller.Name.Split(separators)[1]; sellerHierarchy.LevelDescriptors[1].OrderByKeyExpression = expression2;
Thanks again,
Kevin
After further investigation – this is the case. The OrderByKeyExpression on the FlatDataSource is only applied when it first generates the items as it appears to be part of the generated Cube. Sorting on the different header columns for the measures will overwrite the OrderByKeyExpression as long as the sort is applied. Once the sort is not applied, the hierarchy level will go back to the sort applied to it by the OrderByKeyExpression.
While it does not appear that you can change the OrderByKeyExpression programmatically without essentially building and binding a new FlatDataSource to the XamPivotGrid, you can control the sorts that are applied on the header columns for the measures by calling the Clear method on the FlatDataSource.SortDescriptors and then calling the RefreshGrid() method on the FlatDataSource you have bound. This will cause any UI sorts to be cleared and the OrderByKeyExpressions will be applied again.
Thanks again Andrew.
To elaborate my requirement further using your screen shot. The City has the sort applied to it due to the OrderByKeyExpression set on the FlatDataSource when it first generates its items. The user then clicks on the AmountOfSale column header to sort by AmountOfSale. The user then want to click on the City Header to sort by City once again. It's this last step that I do not have working.
I am finding the same thing. Adding an OrderByKeyExpression works when the FlatDataSource initially generates its items, but does not seem to be respected after that.
I am a little bit confused on the behavior you are seeing / the requirement you have. The OrderByKeyExpression should remain applied, as each Row or Column level in the XamPivotGrid is independent of the other on terms of sorting? For example, see the following screenshot, where the “City” has a sort applied on it for the third letter in each City. Can you please elaborate on the behavior you are looking for here?
I also may have been mistaken in my previous update/recommendation on this matter. The FilterFieldItemControl element is the correct element to be templated, but I am seeing some adverse behaviors on my end when trying to add an OrderByKeyExpression at runtime. I believe this may be something that is evaluated when the FlatDataSource initially generates its items, as I am not able to see the OrderByKeyExpression being respected when added dynamically. I am going to continue to investigate to see if this is the case, but it appears so far that it is.