After reading your question, my understanding is that:
1. You press any char over datagirdview cell and you want to highlight the cell of the grid, which contains that press character inside its content.
Suppose you have data grid view and contains Patient column inside it.
Step 1: Press key "D", so it will highlight the cells of the Patient column of the grid, which contains "D" in their content.
IF this is the requirement:
You can use two events for it as per as my understanding:
1.
DataGridView1.KeyUp
For getting data to highlight.
2.
DataGridView1.CellPainting
For Highlight or paint the data into grid cell.
------------Code.
1. Get the data table.
Function GetTable() As DataTable
' Create new DataTable instance.
Dim table As New DataTable
' Create four typed columns in the DataTable.
table.Columns.Add("Dosage", GetType(Integer))
table.Columns.Add("Drug", GetType(String))
table.Columns.Add("Patient", GetType(String))
table.Columns.Add("Date", GetType(DateTime))
table.Columns.Add("Select", GetType(Boolean))
' Add five rows with those columns filled in the DataTable.
table.Rows.Add(25, "Indocin", "David", DateTime.Now, False)
table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now, False)
table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now, False)
table.Rows.Add(21, "Combivent", "Janet", DateTime.Now, False)
table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now, False)
Return table
End Function
2. Bind DataGridView with a table in form load.
Private Sub WindowFormForGridSearch_Load(sender As Object, e As EventArgs) Handles Me.Load
DataGridView1.DataSource = GetTable()
End Sub
3. Get the data from user enter from key.
Dim l_strSerachData As String
Private Sub DataGridView1_KeyUp(sender As Object, e As KeyEventArgs) Handles DataGridView1.KeyUp
l_strSerachData = e.KeyData.ToString()
DataGridView1.DataSource = GetTable() ' It is bind to call paint.
End Sub
4. Paint the grid cell with l_strSerachData value into "Patient" column of the grid.
Private Sub DataGridView1_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
' Highlight the Patient column data only.
If Me.DataGridView1.Columns("Patient").Index = e.ColumnIndex AndAlso e.RowIndex >= 0 Then
' Data need to highlight into Grid cell.
Dim sw As String = l_strSerachData
If Not String.IsNullOrEmpty(sw) Then
If Not String.IsNullOrWhiteSpace(e.FormattedValue.ToString()) Then
Dim val As String = Replace(DirectCast(e.FormattedValue, String), vbCrLf, String.Empty)
Dim sindx As Integer = val.ToLower.IndexOf(sw.ToLower)
If sindx >= 0 Then
e.Handled = True
e.PaintBackground(e.CellBounds, True)
'the highlite rectangle
Dim hl_rect As New Rectangle()
hl_rect.Y = e.CellBounds.Y + 2
hl_rect.Height = e.CellBounds.Height - 5
'find the size of the text before the search word
'and the size of the search word
Dim sBefore As String = val.Substring(0, sindx)
Dim sWord As String = val.Substring(sindx, sw.Length)
Dim s1 As Size = TextRenderer.MeasureText(e.Graphics, sBefore, e.CellStyle.Font, e.CellBounds.Size)
Dim s2 As Size = TextRenderer.MeasureText(e.Graphics, sWord, e.CellStyle.Font, e.CellBounds.Size)
'adjust the widths to make the highlite more accurate
If s1.Width > 5 Then
hl_rect.X = e.CellBounds.X + s1.Width - 5
hl_rect.Width = s2.Width - 6
Else
hl_rect.X = e.CellBounds.X + 2
hl_rect.Width = s2.Width - 6
End If
'use darker highlight when the row is selected
Dim hl_brush As SolidBrush
hl_brush = New SolidBrush(Color.Yellow)
'paint the background behind the search word
e.Graphics.FillRectangle(hl_brush, hl_rect)
hl_brush.Dispose()
'paint the content as usual
e.PaintContent(e.CellBounds)
End If
End If
End If
End If
End Sub