Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
350
Show top N records
posted

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.

Parents
No Data
Reply
  • 34810
    Offline posted

    Hello Alexander,

    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.

    Sincerely,
    Andrew
    Associate Developer

Children