Manoj S Pant
Прочитав ваш вопрос, я понял, что:
1. Вы нажимаете любой символ над ячейкой datagirdview и хотите выделить ячейку сетки, которая содержит этот символ нажатия внутри своего содержимого.
Предположим, что у вас есть представление таблицы данных и внутри него содержится столбец пациента.
Шаг 1: Нажмите клавишу "D", чтобы она выделила ячейки столбца пациента сетки, который содержит "D" в своем содержимом.
Если это требование:
Вы можете использовать для этого два события в соответствии с моим пониманием:
1.
DataGridView1.KeyUp
Для получения данных для выделения.
2.
DataGridView1.CellPainting
Для выделения или закрашивания данных в ячейку сетки.
------------Код.1. Получить таблицу данных.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. Связать datagridview с таблицей нагрузки в виде.Private Sub WindowFormForGridSearch_Load(sender As Object, e As EventArgs) Handles Me.Load
DataGridView1.DataSource = GetTable()
End Sub
3. Получить данные от пользователей ввода от ключа.// Global variable to hold sreach data.
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. Нарисуйте ячейку сетки со значением l_strSerachData в столбце "пациент" сетки.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