Filter and Search
Data Grid supports the data filtering and searching features, which allow you to locate rows that contain specific text.
Search Panel
The Search Panel helps a user quickly locate rows by the data they contain. When a user types text in the Search Panel, the Data Grid control displays those rows that contain the typed text.
- The search functionality is case-insensitive
- The Contains comparison operator is used to locate the specified text in rows
- The Data Grid control searches all of its columns for the specified text
Set the control's SearchPanelDisplayMode
property (inherited from the DataControlBase
class) to one of the following values to enable the Search Panel:
SearchPanelDisplayMode.Always
— The control permanently displays the Search Panel.SearchPanelDisplayMode.HotKey
— The control displays the Search Panel when a user presses the CTRL+F hotkey. The ESC shortcut clears the Search Panel. A subsequent ESC key press closes the panel. A user can also activate the Search Panel from a column header's context menu.
Related API
DataControlBase.IsSearchPanelVisible
— Gets whether the Search Panel is currently visible.DataControlBase.SearchPanelHighlightResults
— Specifies whether to highlight the search text in the found rows. The property's default value istrue
.DataControlBase.SearchText
— Gets or sets the search text. You can assign a value to this property to filter the control in code. This filtering functionality is supported even if the Search Panel is hidden or disabled (theSearchPanelDisplayMode
property is set toSearchPanelDisplayMode.Never
).DataControlBase.ShowSearchPanelCloseButton
— Allows you to hide the Search Panel's built-in Close button.
Example
The following code enables the Search Panel. The SearchText
property is used to set the search text.
dataGrid.SearchPanelDisplayMode = SearchPanelDisplayMode.Always;
dataGrid.SearchText = "search";
Auto Filter Row
The Auto Filter Row is a special row displayed above all grid rows. It allows a user to type text in its cells to filter data against corresponding columns.
Set the DataGridControl.ShowAutoFilterRow
property to true
to enable the Auto Filter Row.
You can use the ColumnBase.AutoFilterCondition
property to specify the data comparison operation for specific cells (columns) of the Auto Filter Row. When a user types text, the Data Grid control uses the specified comparison operation to compare column values while locating target rows. The following data comparison operations are supported:
Equals
— Row values in the target column should match the entered value.Contains
— Row values in the target column should contain the entered value. This mode is appropriate for String values.StartsWith
— Row values in the target column should start with the entered value. This mode is appropriate for String values.Default
— TheDefault
option is equivalent to theContains
option for the String and Object data types. This option is equivalent to theEquals
option for other data types.
The ColumnBase.AutoFilterValue
property allows you to set a value for a specific Auto Filter Row cell in code. You can use the ColumnBase.AutoFilterValue
to filter the Data Grid even if the Auto Filter Row is hidden.
The filter functionality is case-insensitive.
Example
The following code activates the Auto Filter Row, and displays rows whose values in the 'Name' column start with "M".
using Eremex.AvaloniaUI.Controls.DataControl;
using Eremex.AvaloniaUI.Controls.DataGrid;
dataGrid1.ShowAutoFilterRow = true;
GridColumn colName = dataGrid1.Columns["Name"];
colName.AutoFilterCondition = AutoFilterCondition.StartsWith;
colName.AutoFilterValue = "M";
Filter Rows Dynamically Using an Event
You can handle the CustomRowFilter
event to hide specific rows based on a custom condition.
The CustomRowFilter
event fires for each item in the bound item source in the following cases:
- The control's item source changes.
- The control's rows are filtered (for instance, using the Search Panel and Auto Filter Row).
- The control's
RefreshData
method is called.
Use the SourceItemIndex
event parameter to identify the currently processed item. To hide the corresponding row, set the Visible
event parameter to false
.
Example - Filter Rows with an Event
In the following example, a Data Grid control displays a list of EmployeeInfo objects. The CustomRowFilter
event is handled to implement custom filtration of rows. Rows are hidden according to a value of the EmployeeInfo.EmploymentType property.
It is assumed that the example contains the "Enable Filter" toggle button that activates and deactivates the custom filtration. When the button is clicked, the ToggleButton.IsCheckedChanged
event handler calls the RefreshData
method to refresh grid rows and re-raise the CustomRowFilter
event.
<ToggleButton Name="btnEnableFilter" Content="Enable Filter" IsCheckedChanged="BtnEnableFilter_IsCheckedChanged" />
<mxdg:DataGridControl x:Name="dataGrid" Grid.Row="1" ItemsSource="{Binding Employees}"
CustomRowFilter="DataGrid_CustomRowFilter">
<!-- ... -->
using Eremex.AvaloniaUI.Controls.DataGrid;
private void BtnEnableFilter_IsCheckedChanged(object sender, Avalonia.Interactivity.RoutedEventArgs e)
{
dataGrid.RefreshData();
}
private void DataGrid_CustomRowFilter(object sender, DataGridCustomRowFilterEventArgs e)
{
bool filterEnabled = btnEnableFilter.IsChecked == true;
if (!filterEnabled)
return;
DataGridControl grid = sender as DataGridControl;
IList<EmployeeInfo> dataSource = grid.ItemsSource as IList<EmployeeInfo>;
EmployeeInfo employee = dataSource[e.SourceItemIndex];
e.Visible = employee.EmploymentType != EmploymentType.Contract;
}
Filter in Code
The DataControlBase.SearchText
and ColumnBase.AutoFilterValue
properties can be used to filter the Data Grid control in code, even if the Search Panel and Auto Filter Row are hidden.
If you use the DataControlBase.SearchText
property, the Data Grid filters rows by searching for the entered text in all columns. The Contains
comparison operation is used.
If you use the ColumnBase.AutoFilterValue
property, the Data Grid searches for the entered text in the specified column. The ColumnBase.AutoFilterCondition
property defines the comparison operation.