Private search As String
Private Sub DataGridView1_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
If search = String.Empty OrElse e.ColumnIndex = -1 OrElse e.RowIndex = -1 Then Return
Dim text As String = CStr(e.FormattedValue)
Dim ix As Integer = text.IndexOf(search)
If text = String.Empty OrElse ix = -1 Then Return
Dim ranges As New List(Of CharacterRange)
Do Until ix = -1
ranges.Add(New CharacterRange(ix, search.Length))
ix = text.IndexOf(search, ix + search.Length)
Loop
Dim strFormat As StringFormat = StringFormat.GenericTypographic
strFormat.SetMeasurableCharacterRanges(ranges.ToArray)
' default alignment, translate e.CellStyle.Alignment if needed.
strFormat.LineAlignment = StringAlignment.Center
strFormat.Alignment = StringAlignment.Near
'painting
Dim selected As Boolean = (e.State And DataGridViewElementStates.Selected) = DataGridViewElementStates.Selected
e.PaintBackground(e.ClipBounds, selected)
For Each hilight As Region In e.Graphics.MeasureCharacterRanges(text, e.CellStyle.Font, e.CellBounds, strFormat)
e.Graphics.FillRegion(Brushes.Yellow, hilight)
hilight.Dispose()
Next
strFormat.Dispose()
e.PaintContent(e.ClipBounds)
e.Paint(e.ClipBounds, DataGridViewPaintParts.Border)
e.Handled = True
End Sub