HI,
I have users that would like to sort the rows for Seller Name as in the screen shot. Is this possible?
Hello Kevin,
It is possible to sort the rows that you have shown in your screenshot by using an OrderByKeyExpression in your HierarchyDescriptor for your FlatDataSource. We have documentation that demonstrates how you can do this here: https://es.infragistics.com/help/wpf/xampivotgrid-sorting-rows-and-columns.
Please note that with the FlatDataSource, in order for sorts to take effect, you will need to set the PreserveMembersOrder property of the data source to “false.” Without this setting, the rows and columns will always show in the order they appear in the data source.
Please let me know if you have any other questions or concerns on this matter.
That's great, thank you Andrew. The missing bit of the puzzle was the PreserveMembersOrder property.
Is there a way of customizing the template for the header cell highlighted below? I'd like to put a button in there that the user can click on to sort by seller name.
Thanks again.
I have managed to sort the row elements as required by defining the HierarchyDescriptors as shown on the documentation article when defining my FlatDataSource.
HierarchyDescriptor<Sale> sellerHierarchy = new HierarchyDescriptor<Sale>(p => p.Seller); sellerHierarchy.AddLevel(p => "All sellers", "All sellers"); sellerHierarchy.AddLevel(p => p.Seller.Name, "Seller name"); char[] separators = { ' ' }; Expression<Func<Sale, String>> expression2 = sale => sale.Seller.Name.Split(separators)[1]; sellerHierarchy.LevelDescriptors[1].OrderByKeyExpression = expression2; dataSource.AddHierarchyDescriptor(sellerHierarchy);
Using this code ensures the rows are sorted when the XamPivotGrid is first populated. My problem now is that the user then sorts on a different column, and then wants to sort the rows on the Seller Name again. I can't see how to apply this sort expression again on a subsequent occasion. Any idea?
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.
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.
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.
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
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.