Skip to main content
You are viewing help content for pre-release software. This document and the features it describes are subject to change.
All docs
V26.2
  • Filter Data in Worksheets

    • 5 minutes to read

    The Spreadsheet Document API allows you to apply an AutoFilter to a cell range and hide rows that do not match specified criteria. You can filter by specific values, comparison conditions, top/bottom values, cell or font color, and dynamic criteria such as “Above Average” or “Last Month”.

    Apply an AutoFilter

    Assign an AutoFilter instance to the Worksheet.AutoFilter property. Pass the cell range that contains the data you want to filter, including the header row.

    using System.IO;
    using DevExpress.Docs.Spreadsheet;
    
    // Load a workbook from a file.
    Workbook workbook = new Workbook(File.ReadAllBytes("Document.xlsx"), DocumentFormat.Xlsx);
    
    // Access the first worksheet in the workbook.
    Worksheet worksheet = workbook.Worksheets[0];
    
    // Apply an AutoFilter to a range that includes the header row.
    worksheet.AutoFilter = new AutoFilter(worksheet["A1:C10"]);
    
    using (FileStream stream = File.Create("Document_upd.xlsx"))
    {
        workbook.Save(stream, DocumentFormat.Xlsx);
    }
    

    Use the following AutoFilter properties and methods to manage the filtered range:

    Member Description
    RowIndex, ColumnIndex Return the zero-based position of the filtered range.
    RowCount, ColumnCount Return the size of the filtered range.
    ExpandRange Specifies whether the filtered range automatically expands to include adjacent data.
    Columns Returns the AutoFilterColumnCollection used to specify column filter criteria.
    ClearCriteria Removes filter criteria from all columns and displays all rows.
    Update Re-evaluates filter criteria and updates row visibility. Call this method after you change filter criteria or the underlying data.

    Filter by Column Criteria

    Access a column through the AutoFilter.Columns collection. The collection index is a zero-based offset from the first column in the filtered range. Assign a FilterCriteria descendant to the column’s Criteria property, then call AutoFilter.Update to hide rows that do not match the criteria.

    The following criteria types are available:

    Class Description
    ComparisonFilterCriteria Filters values against one or two comparison conditions (equal to, greater than, and so on).
    ValueFilterCriteria Filters rows in which the cell value is in a specified list of values or date groups (for example, a specific year or month).
    TopBottomFilterCriteria Filters the top or bottom N values or percentage of values.
    DynamicFilterCriteria Filters values with a predefined dynamic condition, such as AboveAverage or LastMonth.
    FillFilterCriteria Filters rows by cell fill color.
    FontColorFilterCriteria Filters rows by font color.

    The following code snippet displays rows in which the value in the second column (“Amount”) is greater than 500:

    worksheet.AutoFilter.Columns[1].Criteria = new ComparisonFilterCriteria(500, FilterComparisonOperator.GreaterThan);
    worksheet.AutoFilter.Update();
    

    Use the ComparisonFilterCriteria(FilterValue, FilterComparisonOperator, FilterValue, FilterComparisonOperator, Boolean) constructor overload to combine two conditions. Set the matchAll parameter to true to require both conditions (“And”), or false to require either condition (“Or”).

    Filter by a List of Values

    The following code snippet displays rows in which the value in the first column (“Region”) is “North” or “West”:

    worksheet.AutoFilter.Columns[0].Criteria = new ValueFilterCriteria("North", "West");
    worksheet.AutoFilter.Update();
    

    To filter dates by year, month, or day components instead of exact date/time values, pass DateGroup objects to the ValueFilterCriteria(values, dateGroups) constructor.

    The following code snippet displays rows in which the value in the third column (“OrderDate”) falls within March 2024:

    worksheet.AutoFilter.Columns[2].Criteria = new ValueFilterCriteria(
        Array.Empty<FilterValue>(),
        new[] { new DateGroup(new DateTime(2024, 3, 1), DateTimeGroupingType.Month) });
    worksheet.AutoFilter.Update();
    

    Filter by Top or Bottom Values

    The following code snippet displays rows with the top 10 values in the “Amount” column:

    worksheet.AutoFilter.Columns[1].Criteria = new TopBottomFilterCriteria(10);
    worksheet.AutoFilter.Update();
    

    Pass bottom: true to filter the bottom values instead of the top values, and percent: true to treat the value as a percentage of the total row count.

    Filter by a Dynamic Condition

    The DynamicFilterType enumeration lists available dynamic conditions, including statistical conditions (AboveAverage, BelowAverage) and date-based conditions (Today, LastWeek, ThisMonth, YearToDate, and others).

    The following code snippet displays rows in which the “Amount” column value is above average:

    worksheet.AutoFilter.Columns[1].Criteria = new DynamicFilterCriteria(DynamicFilterType.AboveAverage);
    worksheet.AutoFilter.Update();
    

    Filter by Cell or Font Color

    The following code snippet displays rows in which the cell in the first column has a light blue fill color:

    using System.Drawing;
    
    worksheet.AutoFilter.Columns[0].Criteria = new FillFilterCriteria(CellColor.FromColor(Color.LightBlue));
    worksheet.AutoFilter.Update();
    

    Clear Filter Criteria

    Call AutoFilterColumn.Clear to remove criteria from a single column, or AutoFilter.ClearCriteria to remove criteria from all columns and display all rows:

    worksheet.AutoFilter.Columns[1].Clear();
    worksheet.AutoFilter.Update();
    
    // Remove criteria from all columns.
    worksheet.AutoFilter.ClearCriteria();
    

    Remove an AutoFilter

    Set the Worksheet.AutoFilter property to null to remove the AutoFilter and display all rows:

    worksheet.AutoFilter = null;