How to: Apply a Dynamic Filter
- 2 minutes to read
This example demonstrates how to apply a dynamic filter to a column.
The dynamic filter allows you to apply one of the predefined date filters to display date values that fall within a specified time period (next, this or last week, month, year, etc.), or to display numbers that are above or below the average.
The dynamic filter criteria can change when the data to which the filter is applied or the current system date changes.
- Turn on the filtering functionality for the required range, as described in the How to: Enable Filtering example.
- Use the AutoFilterBase.Columns property of the SheetAutoFilter object to get a collection of columns in the filtered range (the AutoFilterColumnCollection object). Each column in the collection is defined by the AutoFilterColumn object which provides basic methods for data filtering. To filter data in a particular column, get access to this column by its index in the AutoFilterColumnCollection collection.
To apply a dynamic filter, call the AutoFilterColumn.ApplyDynamicFilter method, and pass the appropriate member of the DynamicFilterType enumeration as a parameter depending on the filter type you wish to apply.
In this example, the two filter types are specified. The first filter criterion displays all the sales values in the third column that are above the average. The second filter criterion displays the reporting dates in the fourth column that occurred this year.
A complete sample project is available at https://github.com/DevExpress-Examples/spreadsheet-document-server-api-part-2-t217615
Worksheet worksheet = workbook.Worksheets["Regional sales"]; workbook.Worksheets.ActiveWorksheet = worksheet; // Enable filtering for the specified cell range. CellRange range = worksheet["B2:E23"]; worksheet.AutoFilter.Apply(range); // Apply a dynamic filter to the "Sales" column to display only values that are above the average. worksheet.AutoFilter.Columns.ApplyDynamicFilter(DynamicFilterType.AboveAverage); // Apply a dynamic filter to the "Reported Date" column to display values reported this year. worksheet.AutoFilter.Columns.ApplyDynamicFilter(DynamicFilterType.ThisYear);