wpf-120197-controls-and-libraries-data-grid-bind-to-data-bind-to-any-data-source-with-virtual-sources-use-virtual-sources-step-3-enable-filter-operations.md
You can allow users to filter rows in the GridControl. Complete the following steps:
Explore the full source code in the following examples and demos:
Infinite Scrolling SourceView Example Run DemoPaged SourceView Example Run Demo
The Issues Service can fetch rows:
public class IssueFilter {
public Priority? Priority { get; private set; }
public DateTime? CreatedFrom { get; private set; }
public DateTime? CreatedTo { get; private set; }
public int? MinVotes { get; private set; }
}
The code snippet below fetches rows without filter conditions:
[Command]
public void FetchIssues(FetchRowsAsyncArgs args) {
args.Result = GetIssuesAsync(args);
}
async Task<FetchRowsResult> GetIssuesAsync(FetchRowsAsyncArgs args) {
var take = args.Take ?? 30;
var issues = await IssuesService.GetIssuesAsync(
skip: args.Skip,
take: take,
sortOrder: GetIssueSortOrder(args.SortOrder),
filter: null
);
return new FetchRowsResult(issues, hasMoreRows: issues.Length == take);
}
Create a filter converter.
Obtain the GridControl filter.
Parse the filter and return an IssueFilter (a type used in the Model). A command bound to the InfiniteAsyncSource.FetchRowsCommand uses the filter when the GridControl fetches rows.
Assign the filter converter to the DataControlBase.CriteriaConverter property. The filter converter specified in the View allows you to avoid a reference to the DevExpress.Data namespace in your ViewModel.
When you specify the DataControlBase.CriteriaConverter property, the FetchAsyncArgsBase.Filter property returns the filter of the Object type. You can cast this filter to the type returned by the filter converter (IssueFilter in this tutorial).
Get the list of priorities to display them in the Priority column drop-down filter:
GetUniqueValues command.[Command]
public void GetUniqueValues(GetUniqueValuesAsyncArgs args) {
if(args.PropertyName == "Priority") {
var values = Enum.GetValues(typeof(Priority)).Cast<object>().ToArray();
args.Result = Task.FromResult(values);
} else {
throw new InvalidOperationException();
}
}
<dxg:GridControl CriteriaConverter="{local:IssueFilterConverter}">
<dxg:GridControl.ItemsSource>
<dx:InfiniteAsyncSource ElementType="{x:Type local:IssueData}"
FetchRowsCommand="{Binding FetchIssuesCommand}"
GetUniqueValuesCommand="{Binding GetUniqueValuesCommand}"/>
</dxg:GridControl.ItemsSource>
<!-- ... -->
</dxg:GridControl>
If a service or database includes a method that obtains unique values, use this method in the GetUniqueValues command.
Allow users to filter GridControl rows by the Priority column as follows:
Set the ColumnBase.AllowedBinaryFilters property to Equals to allow users to display rows with the specified priority.
Set the ColumnBase.FilterPopupMode property to List to enable a drop-down filter that allows users to select one item at a time.
<dxg:GridColumn FieldName="Priority"
AllowedBinaryFilters="Equals"
FilterPopupMode="List"/>
Allow users to filter GridControl rows by the Votes column as follows:
Set the ColumnBase.AllowedBinaryFilters property to GreaterOrEqual to allow users to display rows with votes that are greater than or equal to an input value.
Set the ColumnBase.FilterPopupMode property to Excel to enable a drop-down filter that allows users to create the GreaterOrEqual criteria.
<dxg:GridColumn FieldName="Votes"
AllowedBinaryFilters="GreaterOrEqual"
FilterPopupMode="Excel"/>
Allow users to filter GridControl rows by the Created Date column as follows:
Set the ColumnBase.AllowedDateTimeFilters property to SingleDateRange to allow users to filter rows by a single date or a date range.
Set the ColumnBase.FilterPopupMode property to DateSmart to enable a calendar that allows users to specify dates.
<dxg:GridColumn FieldName="Created"
AllowedDateTimeFilters="SingleDateRange"
FilterPopupMode="DateSmart"/>