簡體   English   中英

突出顯示datagridview單元格中的部分文本

[英]Highlight Part of a text in a cell of datagridview

如何突出顯示datagridview單元格中的部分文本? 我正在使用C#。
例如用戶搜索書。 單元格包含書簽。 我想在書簽中突出顯示“book”。 謝謝。


版。 這段代碼好嗎?

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

我也在尋找一種方法來做到這一點。 在進入明顯的CellPainting事件之后,我發現使用事件的Graphics對象並不能解決問題。 但是,我確實設法使用DataGridView.GetGraphics()方法中的Graphics對象來突出顯示文本的一部分。 我假設你已經知道如何找到包含你搜索的字符串的單元格。 在CellPainting事件中,您要做的第一件事是將單元格繪制為任何其他單元格:

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

接下來要做的是將單元格的文本拆分為2個部分 - 搜索文本之前的部分和搜索文本本身。 你需要這個來計算你想要突出顯示的矩形。

然后使用Graphics對象的MeasureString方法獲取單元格內搜索文本的位置。 由於我使用的是與網格本身相關的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);

從那時起,它只是使用Graphics對象的FillRectangle和DrawString:

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

當然,完成時不要忘記將e的Handled屬性設置為true:

e.Handled = true; 

哦,還有最后一件事:你需要使整個網格無效,或者至少每次搜索新字符串時在上一次搜索中突出顯示的單元格,否則你最終會得到一個網格,其中包含突出顯示的文本與當前搜索字符串無關。

我不認為有任何內置的方法,但我假設您可以處理DataGridViewCellPainting事件,設置e.Handled = true; 然后根據需要自己繪制。

您可以使用PaintBackground來最大限度地減少您自己必須完成的工作量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM