Search as You Type
Showing the Search Panel
As of R1 2016, RadGridView supports searching. Through the new boolean ShowSearchPanel property of the control, the user can show/hide the search panel. Its default value is False. If hidden, the search panel can be shown with the Ctrl+F shortcut.
The default searching behavior has two ways of setting the operator of the filtering criteria, depending on the value type of the property over which the search is performed. For a string and Enum type the operator is set to Contains. For all other types the operator is set to IsEqualTo.
Example 1: Showing the Search Panel
<telerik:RadGridView ItemsSource="{Binding Orders}"
ShowSearchPanel="True"
Name="orderItemsDataGrid" Margin="5"
AutoGenerateColumns="False"
ColumnWidth="*"/>
Figure 1: Showing the Search Panel
Searching over XML data source is available as of R2 2018.
In case highlighting in a custom column or CellTemplate is needed, the HightlightTextBlock can be used. As of R3 2018 its constructor needs to have the SearchStateManager passed as a parameter. It is exposed through the SearchStateManager property of RadGridView. The usage of the HighlightTextBlock is demonstrated in the Custom Highlight Column.
Disable showing the Search Panel
You can control whether the users can use the Search Panel through the CanUserSearch boolean property of RadGridView. Its default value is true. Setting it to false would permanently disable the functionality, meaning that the Ctrl + F combination would not show the panel as well.
Example 2: Disabling the Search Panel
<telerik:RadGridView ItemsSource="{Binding Orders}"
CanUserSearch="False"
Name="orderItemsDataGrid" Margin="5"
AutoGenerateColumns="False"
ColumnWidth="*"/>
Hiding the Close Button
The visibility of the search panel's close button can be controlled via the SearchPanelCloseButtonVisibility property. The default value is Visible meaning that the search panel can originally be closed via this button.
Example 3: Collapse the close button in XAML
<telerik:RadGridView SearchPanelCloseButtonVisibility="Collapsed" />
Example 3: Collapse the close button in code-behind
this.GridView.SearchPanelCloseButtonVisibility = Visibility.Collapsed;
Me.GridView.SearchPanelCloseButtonVisibility = Visibility.Collapsed
Figure 2: Hidden Close Button
Deferred Searching
The deferred searching functionality can be controlled through the IsSearchingDeferred property. Its default value is False and it determines whether the filtering through the search text box will be performed dynamically.
When IsSearchingDeferred is set to True, the filtering will be executed when the value is being committed on lost focus or when the Enter or Tab key is pressed.
Example 3: Setting the IsSearchingDeferred to True
<telerik:RadGridView ItemsSource="{Binding Orders}"
IsSearchingDeferred="True"
Name="orderItemsDataGrid"
Margin="5"
AutoGenerateColumns="False"/>
Commands
Three new commands have been exposed for the text search functionality.
- Search: Executed in order to show the search panel.
- SearchByText: Executed in order to perform an actual search. It takes a string as a parameter - the text to search by.
- CloseSearchPanel: Executed in order to hide the search panel.
Events
As of R2 2016, the SearchPanelVisibilityChanged event will be raised on changing the ShowSearchPanel property. Its arguments are of type VisibilityChangedEventArgs and contain the value of the new visibility - NewVisibility.
A common scenario where you can use this event is when you want to clear the search criteria on collapsing the panel:
Example 4: Clearing search criteria on SearchPanelVisibilityChanged
private void RadGridView_SearchPanelVisibilityChanged(object sender, VisibilityChangedEventArgs e)
{
if (e.NewVisibility == Visibility.Collapsed)
{
var clearSearchValue = GridViewSearchPanelCommands.ClearSearchValue as RoutedUICommand;
clearSearchValue.Execute(null, this.RadGridView.ChildrenOfType<GridViewSearchPanel>().FirstOrDefault());
}
}
Private Sub RadGridView_SearchPanelVisibilityChanged(sender As Object, e As VisibilityChangedEventArgs)
If e.NewVisibility = Visibility.Collapsed Then
Dim clearSearchValue = TryCast(GridViewSearchPanelCommands.ClearSearchValue, RoutedUICommand)
clearSearchValue.Execute(Nothing, Me.RadGridView.ChildrenOfType(Of GridViewSearchPanel)().FirstOrDefault())
End If
End Sub
In R1 2020, the Searching and Searched events were introduced.
The Searching event will be raised when the grid data is about to be searched. It's arguments are of type GridViewSearchingEventsArgs and contain the value of the text which was entered in the search panel TextBox - SearchText. They also contain a boolean property which indicates whether the event should be canceled - Cancel.
A common scenario for the use of this event is when you want to cancel the search based on a condition:
Example 5: Stop the searching based on a condition
private void RadGridView_Searching(object sender, Telerik.Windows.Controls.GridView.GridViewSearchingEventArgs e)
{
if (e.SearchText.ToString() == "SomeText")
{
e.Cancel = true;
}
}
The Searched event will be raised when the grid data has been searched. It's arguments are of type GridViewSearchedEventArgs and contain the value of the text which was entered in the search panel TextBox - SearchText.
For more information, refer to the Overview article.
Modifying the Searching Criteria
In order to modify the search behavior, you can benefit from the following three search operators:
-
+: The items that will pass the filtering operation will have to contain both the value before the operator and the one after it.
Figure 2: Using the
+
search operator -
-: All items that will pass the filtering operation will have to contain the value before the operator, but not the one after it.
Figure 3: Using the
-
search operator -
"": When a word or a phrase is put in quotes, the filtered objects will contain only the exact same value.
Figure 4: Using the
""
search operator
SearchMode
With R2 2019 we introduced a new SearchMode property which allows you to control how items are matched when search is executed. It has the following two possible values:
MatchAnyTerm: Items match the search operation when they fulfill any of the search terms. For example, if
John Terry
is inputted in the search panel, items containing any of the terms "John" and "Terry" in any of their properties will be matched. This is the default value.MatchAllTerms: Items match the search operation only when they fulfill all of the search terms. Continuing with the previous example, if
John Terry
is entered as the search text, only items which contain both terms ("John" and "Terry") in any of their properties will be matched.MatchExact: Items match the search operation only when they exactly match the search text.
Change the Label Text of the Search Panel
By default, the value of the TextBlock appearing before the Search TextBox in the SearchPanel is "Full Text Search". It can be altered through the Localization mechanism of RadGridView. The resource key that needs to be modified is GridViewSearchPanelTopText. More information can be found in the Localization topic.
Add Search Criteria Programmatically
RadGridView's search mechanism supports defining a search criteria programmatically. This can be done through the SearchByText Command. More information can be found in the Commands Overview topic.
Search Over Dynamic Data
As of R2 2018 RadGridView provides full support for searching over dynamic data. This includes IDynamicMetaObjectProvider(DynamicObject and ExpandoObject), ICustomTypeProvider and ICustomTypeDescriptor implementations.
Search in Hidden Columns
RadGridView's text search mechanism supports searching in hidden columns. This behavior can be enabled by setting the CanUserSearchInHiddenColumns property of the RadGridView control to True.
This feature was first introduced with the non-official version 2017.3.1127 of R3 2017. Afterwards, it was included in R1 2018 official release of the Telerik UI for WPF suite.
Example 6: Setting CanUserSearchInHiddenColumns property in XAML
<telerik:RadGridView CanUserSearchInHiddenColumns="True"/>
Search With Accent Insensitive
With R1 2020 we introduced a new boolean IsSearchWithAccentEnabled property of SearchStateManager which allows you to search with accent insensitive. This behavior is turned off by default. To enable this functionality you can subscribe to the Loaded event of the RadGridView. In the event handler, you can set this property to True.
Example 7: Setting IsSearchWithAccentEnabled property
private void GridView_Loaded(object sender, RoutedEventArgs e)
{
var gridView = sender as RadGridView;
gridView.SearchStateManager.IsSearchWithAccentEnabled = true;
}
Figure 4: Using Search With Accent Insensitive