I am trying to find out a way to show only N top records from the datasource with respect to currently set filter conditions and sort order.
I.e. if there are records with Amount value in range of 1..100 then if sorted by Amount ASC i need to display, say, records with 1..10. When sorted by Amount DESC , records 91-100 are to be displayed. If filter by Amount>50, then 51..60 should be displayed.
It look as if I need to setup a filter on VisibleIndex value yet I couldn't find a way to do so.
Any ideas are appreciated.
Hello Alexander,
I have been discussing this Top and Bottom filter issue with my teammates, but the behavior here actually appears to be expected. As a little more insight as to what this behavior is, let's take your scenario where you have records numbered 1 - 100. Now, let's say you wanted to only show the first 10 records where a value was greater than 50 by applying a filter with the two following conditions:
Greater Than 50,Bottom 10.
This would result in an empty XamDataGrid. The reason for this, is because the Top and Bottom comparison conditions enumerate over the full list of data items in the XamDataGrid, and so the Bottom 10 here would be records 1 - 10, which would be filtered out by the Greater Than condition, and the Greater Than 50 condition would be records 51 - 100, which would be filtered out by the Bottom 10 condition. These essentially cancel each other out and result in an empty grid.
To achieve your requirement on this matter, I would recommend that you continue with the Collection View methods that I had mentioned earlier in this thread. Please let me know if you have any other questions or concerns on this matter.
Sincerely,AndrewAssociate Developer
Hi Andrew,
Thanks a lot. I've already begun to look into CollectionView direction.
I'd appreciate if you could CC me on the Top filter issue tracker so I'll be notified once it is sorted out.
Alex
Thank you for your post.
I have been investigating into this issue, and for this to work, it appears that you will need to subset the data that you show in the XamDataGrid both on filtering and on sorting. To achieve your requirement, I would recommend that you begin by setting the SortEvaluationMode and the FilterEvaluationMode properties of the XamDataGrid.FieldLayoutSettings to "Manual." What this will do, is it will require you to perform the sorting and filtering operations on your bound collection, and it will essentially disband the built-in sorting and filtering operations on the XamDataGrid. By handling the Sorted event and the RecordFilterChanged event of the grid, you can catch when your filter changes or when a sort is applied.
I would also recommend usage of a collection view so that you will not need to rebind the DataSource of the XamDataGrid. When the Sorted event mentioned above fires, you can get the FieldSortDescription that has been applied from the event arguments' SortDescription property. This FieldSortDescription element has information related to the Field being sorted and the direction of the sort. Using these, you can create your own SortDescription for use on the collection view used for your data source. You will also need to filter the collection view to show only the top N records in your grid. I have found an external article that may help you to show the top N items of a collection view, here: http://stackoverflow.com/questions/19838907/show-top-x-items-of-icollectionview.
Regarding the actual filtering operations of the XamDataGrid, you can handle the RecordFilterChanged event of the grid and examine the RecordFilter element that was changed. You can obtain this element from the e.RecordFilter property of the event arguments of that event. This RecordFilter element has a Conditions collection that contains the ComparisionCondition elements that have more information on your filter. Using linq as shown in the thread mentioned above, you can examine the filter to be applied in the XamDataGrid, and create your own function that filters your collection view and takes the top 10 elements with respect to the filter that you are applying.
It is also worth mentioning that the XamDataGrid has a Top and Bottom filter operand that I normally would recommend using with this. The issue here, is that it does not appear to take the filtering conditions into account. You may be able to construct a RecordFilter element that you can add to your FieldLayout's RecordFilters collection to use this with the sorting part of your requirement, but as the filtering conditions are not currently being taken into account, you will need to externally filter there. These RecordFilter elements can be constructed like so, where "XDG1" is the XamDataGrid:
FieldLayout layout = XDG1.FieldLayouts[0];
RecordFilter filter = new RecordFilter();filter.Conditions.Add(new ComparisonCondition(ComparisonOperator.Top, 10));filter.Field = layout.Fields["myFieldName"];
layout.RecordFilters.Add(filter);
I believe that these "Top" and "Bottom" filter operands not taking other filter operations into account may be a bug with the XamDataGrid, and I am continuing to investigate this issue.
Please let me know if you have any other questions or concerns on this matter.