Back to Devexpress

How to: Filter Top or Bottom Ranked Values

officefileapi-113765-spreadsheet-document-api-examples-filter-data-how-to-filter-top-or-bottom-ranked-values.md

latest2.3 KB
Original Source

How to: Filter Top or Bottom Ranked Values

  • Sep 19, 2023
  • 2 minutes to read

This example demonstrates how to display the top/bottom ranked values using the "Top 10" filter.

  1. Turn on the filtering functionality for the required range, as described in the How to: Enable Filtering example.
  2. 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.
  3. Call the AutoFilterColumn.ApplyTop10Filter and pass the following parameters.

View Example

csharp
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 filter to the "Sales" column to display the top ten values.
worksheet.AutoFilter.Columns[2].ApplyTop10Filter(Top10Type.Top10Items, 10);
vb
Dim worksheet As Worksheet = workbook.Worksheets("Regional sales")
workbook.Worksheets.ActiveWorksheet = worksheet

' Enable filtering for the specified cell range.
Dim range As CellRange = worksheet("B2:E23")
worksheet.AutoFilter.Apply(range)

' Apply a filter to the "Sales" column to display the top ten values.
worksheet.AutoFilter.Columns(2).ApplyTop10Filter(Top10Type.Top10Items, 10)