Step 5: Consider Sort and Filter Restrictions
- 3 minutes to read
If you have successfully completed the preceding steps, the GridControl‘s UI allows end-users to perform data management operations. The Issues Service, however, has the certain sort and filter restrictions:
- Hot and Week sort orders can be filtered by Tags only.
- You can filter by Votes only after sorting by Votes.
Note
The Issues Service is used as an example of a data source in this tutorial.
An error occurs if you make a request that violates the restrictions above:
static void CheckRestrictions(IssueSortOrder sortOrder, IssueFilter filter) {
if(filter == null)
return;
if((sortOrder == IssueSortOrder.Hot || sortOrder == IssueSortOrder.Week)
&& (filter.CreatedFrom != null || filter.CreatedTo != null || filter.MaxVotes != null || filter.MinVotes != null))
throw new InvalidOperationException("Restrictions violation");
if((filter.MaxVotes != null || filter.MinVotes != null)
&& !(sortOrder == IssueSortOrder.VotesAscending || sortOrder == IssueSortOrder.VotesDescending))
throw new InvalidOperationException("Restrictions violation");
}
The following image shows an error that occurs if you apply a filter by Votes that is incompatible with the Hot sort order:
To not show such error messages, make the GridControl discard operations restricted by the data service. This topic explains how to do it.
To Consider Sort and Filter Restrictions
Create a function that implements the service’s restrictions and determines whether sort and filter operations are compatible:
static bool IsValidFilter(string filterProperty, string sortProperty) { if(filterProperty == "Tags") return true; if(sortProperty == "Hot" || sortProperty == "Week") return false; if(filterProperty == "Votes" && sortProperty != "Votes") return false; return true; }
Handle the DataControlBase.FilterGroupSortChanging event to discard filters that are incompatible with sorting:
void OnFilterGroupSortChanging(object sender, FilterGroupSortChangingEventArgs e) { // ... var sortProperty = e.SortInfo.SingleOrDefault()?.PropertyName; var invalidFilters = e.SplitColumnFilters.Keys .Where(key => !IsValidFilter(key, sortProperty)) .ToArray(); foreach(var invalidFilter in invalidFilters) e.SplitColumnFilters.Remove(invalidFilter); }
Example 1
A filter by Votes is discarded if you perform the following steps:
- Sort by Votes
- Filter by Votes
- Sort by Created
This example shows that changing a sort mode discards incompatible filters.
Example 2
A filter by Votes is discarded if you perform the following steps:
- Sort by Created
- Filter by Votes
In this example, applying a filter by Votes makes no sense (the Issues Service can filter by Votes only after sorting by Votes). Follow the next step to prohibit such actions.
Prohibit end-users from applying filters that violate the Issues Service‘s restrictions:
<dxg:GridColumn FieldName="Created" AllowColumnFiltering="{DXBinding '$dxu:DefaultBooleanExtension.ToDefaultBoolean(!@e(hotColumn).IsSorted and !@e(weekColumn).IsSorted)'}" /> <dxg:GridColumn FieldName="Votes" AllowColumnFiltering="{DXBinding '$dxu:DefaultBooleanExtension.ToDefaultBoolean(@Self.IsSorted)'}" />
The image below shows the result:
To Continue or Review
- To go to the next tutorial, see How to Bind to IQueryable.
- To return to the previous tutorial step, see Step 4: Enable Search Panel.