VB.net: Highlighting a search term in a DataGridView

I’m building a search form into an application that has a database back end. I managed to configure a nice little search which takes some user input, and then modifies the results shown in a DataGridView control. However, not being satisfied with just showing a subset of results, I wanted to be able to highlight the values that had matched so that it was clearer for the end user to see why the records still in the view were there.

This, it turns out, is not as simple as I hoped. However, I have now got it working. The form has a text box which has a sub for validating the input. This runs the actual search. The bit we are interested in here though is that we handle the CellPainting event on the DataGridView control, and then customise the painting of the control to meet our needs.

To give you an idea of what the highlighted form looks like:Highlighting in a DataGridView

 

This is the code that does the work. It is designed to not be case sensitive, and to pick up multiple occurrences of a string in the cell. It is well commented so that you can see what is going on:

5 people found this post useful.


12 thoughts on “VB.net: Highlighting a search term in a DataGridView

  1. Great post, really helpful.

    Did some changes as follows to achieve highlight of multiple words in search string. Hope this will help someone.

    Private Sub dgv_Results_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DGVSearch.CellPainting

    ‘If there is no search string, no rows, or nothing in this cell, then get out.
    If TextBox1.Text = String.Empty Then Return
    If (e.Value Is Nothing) Then Return
    If e.RowIndex < 0 Or e.ColumnIndex < 0 Then Return
    ' Create an array of words
    Dim s() As String = TextBox1.Text.Split(" ")

    e.Handled = True
    e.PaintBackground(e.CellBounds, True)
    'Looping through each word
    For Each n As String In s

    If n = "" Then

    Else

    'Get the value of the text in the cell, and the search term. Work with everything in lowercase for more accurate highlighting
    Dim str_SearchTerm As String = n.Trim.ToLower

    'rest is same, just add "End if" and "Next" at the end

  2. Hello,
    Thanks for the code.
    I’m new to programming. How to fire the event dgv_Results_CellPainting?
    Thanks in advance,
    thenndral.

    1. The event is fired by adding ‘Handles’ to the sub. In the example above my DataGridView was called dgv_Results and the action was CellPainting.

      Private Sub dgv_Results_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles dgv_Results.CellPainting

      Alternatively you can define the sub with no handles and then create the event handlers at runtime. This is covered here: https://msdn.microsoft.com/en-us/library/dfty2w4e%28v=vs.110%29.aspx

  3. can i use this in framework 3.0 and vs2005 in c#
    when i am working with this (For Each HLRange In HLRanges) In this line it is given error that type is not specified please help what do i do?

    1. It should work fine in .Net 3.0 and VS2005. This code is VB.Net though and would need rewriting to use C#.

        1. I see. I never tested with any images in my dataset. The only thing I could suggest trying is ignoring columns with images in. Try this line just before e.Handled = true:

          If e.ColumnIndex = 2 Then Return

          Change the number to the index of the column with the image in and see how you get on.

          (Note: not tested at all – just a hunch!)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.