Select a piece of text in a datagridview cell

How can I highlight a piece of text in a datagridview cell? I am using C #.
For example, a user is looking for a book. the cells contain a bookmark. I want to highlight the book in the bookmark. Thank.


Release. Is this code ok?

Private Sub DataGridView1_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting

    If e.RowIndex >= 0 And e.ColumnIndex >= 0 Then

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

        Dim sw As String = GetSearchWord(e.ColumnIndex)
        If Not String.IsNullOrEmpty(sw) Then

            Dim val As String = DirectCast(e.FormattedValue, String)

            Dim sindx As Integer = val.ToLower.IndexOf(sw.ToLower)
            If sindx >= 0 Then
                '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
                If ((e.State And DataGridViewElementStates.Selected) <> DataGridViewElementStates.None) Then
                    hl_brush = New SolidBrush(Color.DarkGoldenrod)
                Else
                    hl_brush = New SolidBrush(Color.LightGoldenrodYellow)
                End If

                'paint the background behind the search word
                e.Graphics.FillRectangle(hl_brush, hl_rect)

                hl_brush.Dispose()
            End If
        End If

        'paint the content as usual
        e.PaintContent(e.CellBounds)
    End If
End Sub
+3
source share
2 answers

I donโ€™t think there are any built-in ways to do this, but I would suggest that you can handle the event CellPaintingfor DataGridView, set e.Handled = true;, and then draw it yourself, as you need it.

, PaintBackground, , .

+2

. CellPainting, , Graphics Graphics . Graphics DataGridView.GetGraphics(), . , , , , . CellPainting , , :

 e.Paint(e.ClipBounds, DataGridViewPaintParts.All);

, , - 2 - . , , .

MeasureString Graphics, . Graphics, , Graphics , . DataGridView.GetCellDisplayRectangle, :

 CellRectangle = Cell.DataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
 HighLightedRect = new Rectangle((Point)SizeBeforeHighLight, HighLightedSize);
 HighLightedRect.Location = new Point(CellRectangle.Location.X + SizeBeforeHighLight.Width, CellRectangle.Location.Y + Cell.ContentBounds.Top);

FillRectangle DrawString Graphics:

 g.FillRectangle(new SolidBrush(Color.Black), HighLightedRect);
 g.DrawString(HighlighetText, dgvGrid.Font, new SolidBrush(Color.White), HighLightedRect.Location);
 g.Flush();

, , e true, :

e.Handled = true; 

, : , , , , , , .

+3

Source: https://habr.com/ru/post/1758890/


All Articles