Highlight a text in the cell in data gridview

  • Thread starter Thread starter ahmeddc
  • Start date Start date
A

ahmeddc

Guest
hi

i want to Highlight a text in the cell in datagridview

i use this code but not work for arabic string work only english

how to edit with code to work both language arabic and also english

Dim dt As DataTable = New DataTable
Private IsSelected As Boolean = False

Private Sub SearchStringPosition(ByVal Searchstring As String)
Me.IsSelected = True
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dt.Columns.Add("name")
dt.Columns.Add("address")
dt.Rows.Add("arun", "Palakkad")
dt.Rows.Add("سعيد", "Karaikudi")
dt.Rows.Add("ilavarasan", "جابر")
dt.Rows.Add("prithiviraj", "chennai")
dt.Rows.Add("vignesh", "pandicherry")
dataGridView1.DataSource = dt
End Sub
Private Sub dgv_Results_CellPainting(ByVal sender As Object, ByVal e As DataGridViewCellPaintingEventArgs) Handles dataGridView1.CellPainting
Try
'If there is no search string, no rows, or nothing in this cell, then get out.
If txtSearch.Text = String.Empty Then Return
If (e.Value Is Nothing) Then Return
If e.RowIndex < 0 Or e.ColumnIndex < 0 Then Return

e.Handled = True
e.PaintBackground(e.CellBounds, True)

'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 = txtSearch.Text.Trim.ToLower
Dim str_CellText As String = DirectCast(e.FormattedValue, String).ToLower

'Create a list of the character ranges that need to be highlighted. We need to know the start index and the length
Dim HLRanges As New List(Of CharacterRange)
Dim SearchIndex As Integer = str_CellText.IndexOf(str_SearchTerm)
Do Until SearchIndex = -1
HLRanges.Add(New CharacterRange(SearchIndex, str_SearchTerm.Length))
SearchIndex = str_CellText.IndexOf(str_SearchTerm, SearchIndex + str_SearchTerm.Length)
Loop

' We also work with the original cell text which is has not been converted to lowercase, else the sizes are incorrect
str_CellText = DirectCast(e.FormattedValue, String)

' Choose your colours. A different colour is used on the currently selected rows
Dim HLColour As SolidBrush
If ((e.State And DataGridViewElementStates.Selected) & DataGridViewElementStates.None) Then
HLColour = New SolidBrush(Color.DarkGoldenrod)
Else
HLColour = New SolidBrush(Color.BurlyWood)
End If

'Loop through all of the found instances and draw the highlight box
For Each HLRange In HLRanges

' Create the rectangle. It should start just underneath the top of the cell, and go to just above the bottom
Dim HLRectangle As New Rectangle()
HLRectangle.Y = e.CellBounds.Y + 2
HLRectangle.Height = e.CellBounds.Height - 5

' Determine the size of the text before the area to highlight, and the size of the text to highlight.
' We need to know the size of the text before so that we know where to start the highlight rectangle
Dim TextBeforeHL As String = str_CellText.Substring(0, HLRange.First)
Dim TextToHL As String = str_CellText.Substring(HLRange.First, HLRange.Length)
Dim SizeOfTextBeforeHL As Size = TextRenderer.MeasureText(e.Graphics, TextBeforeHL, e.CellStyle.Font, e.CellBounds.Size)
Dim SizeOfTextToHL As Size = TextRenderer.MeasureText(e.Graphics, TextToHL, e.CellStyle.Font, e.CellBounds.Size)

'Set the width of the rectangle, a little wider to make the highlight clearer
If SizeOfTextBeforeHL.Width > 5 Then
HLRectangle.X = e.CellBounds.X + SizeOfTextBeforeHL.Width - 6
HLRectangle.Width = SizeOfTextToHL.Width - 6
Else
HLRectangle.X = e.CellBounds.X + 2
HLRectangle.Width = SizeOfTextToHL.Width - 6
End If

'Paint the highlight area
e.Graphics.FillRectangle(HLColour, HLRectangle)
Next

'Paint the rest of the cell as usual
e.PaintContent(e.CellBounds)

Catch ex As Exception
Exit Sub
End Try

End Sub

Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
On Error Resume Next
SearchStringPosition(txtSearch.Text)
dataGridView1.Refresh()
End Sub

Continue reading...
 
Back
Top