In the 2014 Q2 release, we introduced two features that will greatly speed up your end-users’ working process with the data grid—a search functionality and a check-all checkbox in the column header.

Search Row in RadGridView for WinForms by Telerik

Build-in search functionality

Thanks to the friendly UI, the search functionality of the grid allows end-users to quickly navigate to cells that interest them. Searching through tens of thousands of cells takes time and using the main thread for time consuming tasks is never advisable, so this is why we implemented the search in a separate thread. This leaves the UI responsive and at the same time increases the speed of the search. In addition, there is a public API which gives access to the search mechanism, so you can utilize the functionality with or without showing the search row in the grid UI.  

A valid alternative solution would be to filter the rows according to certain criteria. However, one filter criteria concerns just one column, while the search looks up all columns at once. Moreover, you may not want to hide and then show different rows upon changing the filter criteria, but instead always have all the rows visible.

Let’s now take a look at the options available to your end-users and how you can control their search behavior.

End-user features

The end-user controls the search operation via the so called search row. In order to allow users to use the search row, you have to execute this single line of code:

this.radGridView1.AllowSearchRow = true;
Search in RadGridView for WinForms by Telerik  
In the search row, the users can find controls to enter their search criteria, toggle case sensitivity and navigate back and forth through the search results. When a user starts typing in the search box the results are immediately highlighted inside the cells. Once the user finishes typing, he can use either the previous\next buttons or the Enter and Shift+Enter key combination to navigate back and forth through the results. If at any point he moves the selection away from the currently highlighted results, the next time he moves back or forth, the result that will be highlighted will be relative to the current selection.

Now let’s see how you can control the behavior of the search functionality so that it fits your specific needs.

Intuitive and flexible API


First of all, you can access the logical search row through the MasterView of RadGridView: 

GridViewSearchRowInfo searchRow = this.radGridView1.MasterView.TableSearchRow;

Furthermore, to start a simple search job you should just call the Search method of that row:

radGridView1.MasterView.TableSearchRow.Search("new york");

For your convenience, there are a number of options available at RadGridView level  by which you can fine tune the search mechanism.
  • CaseSensitive - Defines whether searching will be case sensitive or case insensitive. The same option that end-users have with the button on the search row element.
  • Culture - Defines which CompareInfo object will be used for searches when CaseSensitive is set to false.
  • CompareOptions – A CompareOptions value that defines how strings will be compared when CaseSensitive is set to false.
  • HighlightResults – Determines whether results will be highlighted within cells.
  • AutomaticallySelectFirstResult – Determines if the first result found by the search mechanism will be selected and brought into view immediately after it is found.

The SelectNextSearchResult and SelectPreviousSearchResult methods can be used for navigation through the search results. 

radGridView1.MasterView.TableSearchRow.SelectNextSearchResult();
radGridView1.MasterView.TableSearchRow.SelectPreviousSearchResult();
This is fine if you are,for example, providing an alternative search UI and do not need the actual results. If you do want the results, though, you have to subscribe to the SearchProgressChanged event. 

Multi-threading enhancements and considerations


Before we get to the event handler of the event let’s look at some more internal mechanics of the search mechanism.   The search is executed on another thread so it would not hamper the responsiveness of the UI. One interesting thing we found during the development is that if you have many search results and you notify the UI thread of every “finding” immediately, you end up making so many invokes that the main thread is blocked handling them anyway. So we have a property called InitialSearchResultsTreshold. This is an integer number defining a number of search results that will be returned one by one. This way the search will very quickly display the initial results. The user would probably not care about the 100th and up result right after he inputs something, so after this number is reached search results are returned in groups. The size of the groups is controlled through the SearchResultsGroupSize property. The default values for these properties are 100 and 111 respectively. You may wonder why 111? It’s actually a simple trick to hide the grouping from the users. When you get lots of search results the label inside the search box updates with the new number very quickly, it becomes pretty much unreadable. If your group size is 100 the last two digits in this label will freeze until the last result group is return and only if its size is not 100. When the group size is 111 all digits “spin” all the time and it just looks better.

Now let’s get back to the event handler for the SearchProgressChanged event. You can have up to three cases here:

  1. The initial moment when results are returned one by one. In this case the event arguments have the Cell property which will be assigned with the new found cell. The SearchFinished property will be false. 
  2. After the initial threshold is reached the results will be in groups and will be available through the Cells property of the event arguments. The SearchFinished property will be false. 
  3. When the search is finished both Cell and Cells will be null and the SearchFinished will be true.

Coming from the above facts we can extract a couple of tips. Therefore, if you want all the results displayed one by one you should set the initial threshold to a big value. If you want the results all at once you should set it to 0 and the group size to a big value. 

Now, if your end-users happen to lose something, they should not worry – the Search functionality have them covered. 

“Check all” check box in GridViewCheckBoxColumn

We noticed a very high interest in a KB article of ours regarding “check all” functionality in RadGridView and with the Service pack of Q2 2014 we added this functionality (GridViewCheckBoxColumn) to RadGridView to save you the time of embedding it. Now, you just need to set a single property and your column will show a check box in the header cell. This functionality applies to a template level, so you can use it in hierarchy too:

GridViewCheckBoxColumn checkBoxColumn = (GridViewCheckBoxColumn)radGridView1.Columns["CheckBox"];
checkBoxColumn.EnableHeaderCheckBox = true;
CheckAll in RadGridView for WinForms by Telerik  
I hope you found this to be a good read that will enable you to easily implement our features in your project. Stay tuned for more blog posts reviewing the following additions to the suite:
  • A refactored version of RadScheduler
  • Document Processing libraries
  • Multi-line support in RadPageView
  • Freely typed dates in RadMaskedEditBox/RadDateTimePicker

Happy coding!



About the Author

Ivan Petrov

Ivan Petrov is a Software Developer at Telerik’s WinForms DevLabs. He joined the company back in 2011 after graduating from Telerik Academy. He is an engineer of Computer Systems and Technologies and is currently working towards his master degree. In his spare time, Ivan is a true petrolhead and an avid gamer who also enjoys playing rugby, basketball, football/soccer games.

Related Posts

Comments

Comments are disabled in preview mode.