RadGridView offers a build-in search functionality available for both end users and developers. The search mechanism executes in a separate thread which leaves the UI responsive at all times. To enable the search row for end users all you have to do is set the AllowSearchRow property of RadGridView to true:
this.radGridView1.AllowSearchRow = true;
Me.RadGridView1.AllowSearchRow = True
As of R2 2019 SP GridViewSearchRowInfo offers the DeferredSearch property which indicates whether the grid control will wait until the
Enterkey is pressed before a new search operation is started. Its default value is false.
The end user can select in which columns to perform searching by the provided Search in columns drop down. Thus, the highlighted result will be displayed in the specified columns only. You can specify whether the search functionality is enabled for a given column by setting the column's AllowSearching property.
Since R2 2018 SP1 RadGridView has a property IsSearchAsync which controls whether the search will be performed on a background thread [true] or on the main thread [false].
The available properties to tweak the search experience and performance and how to access them are outlined below. The API is in the GridViewSearchRowInfo object which can be accessed through the grid view MasterView.
|CaseSensitive||This property defines whether searching will be case sensitive or case insensitive.|
|Culture||The culture which CompareInfo object will be used for searches when CaseSensitive is set to false.|
|CompareOptions||A CompareOptions value that defines how search will be performed when CaseSensitive is set to false.|
|HighlightResults||Determines whether results will be highlighted with cells.|
|AutomaticallySelectFirstResult||Determines if the first result found by the search mechanism will be selected and brought into view.|
|ShowClearButton||Gets or sets a value indicating whether to show the clear button.|
|ShowCloseButton||Gets or sets a value indicating whether to show the close button.|
|CloseOnEscape||Gets or sets a value indicating whether to close the search row when escape key is pressed.|
|IsSearchAsync||Gets or sets a value indicating whether the search will be performed on a background thread [true] or on the main thread [false].|
|SearchDelay||Gets or sets a value indicating how long the grid will wait after a key is pressed until it starts a search.|
|DeferredSearch||Gets or sets a value indicating whether grid will wait until Enter key is pressed before it starts a search.|
Fwill show the search row if it is hidden. The
Esckey will close the search row.
The ContextMenuOpening event is fired when the settings drop down is about to be shown. The ContextMenuOpeningEventArgs.ContextMenuProvider is the GridSearchCellElement in this case.
To change the highlight color you should use the HighlightColor property of the TableElement.
radGridView1.TableElement.SearchHighlightColor = Color.LightBlue;
RadGridView1.TableElement.SearchHighlightColor = Color.LightBlue
You can also use the search functionality programmatically without showing the search row, just by using its API.
GridViewSearchRowInfo searchRow = this.radGridView1.MasterView.TableSearchRow;
Dim searchRow As GridViewSearchRowInfo = Me.RadGridView1.MasterView.TableSearchRow
To manually search, call the Search method and pass the search criteria as a parameter. To get the search results you have to subscribe to the SearchProgressChanged event. In the event handler you will have to handle three cases:
Initially the search results are returned one at a time. This allows you to quickly get the first results as they are discovered. In this case the Cell property of the SearchProgressChangedEventArgs will contain the cell that was found. You can control how many results are returned one by one through the InitialSearchResultsTreshold property of the search row. If you set the property to 0 you will skip this case entirely.
After the threshold is reached results are returned in groups of cells. This improves performance as less calls are required from the search thread to the main thread. In this case the Cells property of SearchProgressChangedEventArgs will contain a collection of cells that matched the search criteria. To control the size of the Cells collection you can use the SearchResultsGroupSize property of the search row. If the InitialSearchResultsTreshold is greater than the total search results you might not fall in this case. If the search cache already contains the results for a given search criteria this cache will be return as a collection right away.
The search is complete. In this case the SearchFinished property will return true.
In (Table 1.) you can see an overview of the values of the properties in the above cases.
|Until threshold is reached||GridSearchResultCellInfo||null||false|
|After threshold is reached||null||GridSearchResultCellCollection||false|
The search mechanism of RadGridView searches in group rows as well as data rows. Since there are no cells respectively columns in group rows the ColumnInfo property of the GridSearchResultCellInfo will be
null for group rows.
If you start a search operation before a previous one is complete the old one will be stopped and the new one will be started immediately. If a search operation successfully finishes the results from it are cached for subsequent searches.
You can suspend/resume the search temporarily by using the SuspendSearch and ResumeSearch methods. The IsSearchSuspended indicates if the search is currently suspended.