突出显示文本的一部分在DataGridView中的单元格 [英] Highlight Part of a text in a cell of datagridview

查看:145
本文介绍了突出显示文本的一部分在DataGridView中的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能突出显示DataGridView中的单元格文本的一部分?
我使用C#。结果
例如用户搜索的书。对细胞的包含书签。我想在书签突出书。
感谢。






版。
是这个代码可以吗?



 私人小组DataGridView1_CellPainting(BYVAL发件人为对象,BYVAL E上System.Windows.Forms的.DataGridViewCellPaintingEventArgs)处理DataGridView1.CellPainting 

。如果e.RowIndex> = 0和e.ColumnIndex> = 0,那么

e.Handled = TRUE
é .PaintBackground(e.CellBounds,真)

尺寸SW的String = GetSearchWord(e.ColumnIndex)
。如果不String.IsNullOrEmpty(SW),然后

暗淡VAL的String = DirectCast(e.FormattedValue,字符串)

昏暗sindx为整数= val.ToLower.IndexOf(sw.ToLower)
如果sindx> = 0,那么
在矩形的HIGHlite
昏暗hl_rect作为新的矩形()
hl_rect.Y = e.CellBounds.Y + 2
hl_rect.Height = e.CellBounds.Height - 5

'之前找到搜索词
的文字大小和搜索词
昏暗sBefore的String = val.Substring(0,sindx)
昏暗剑作为大小字符串= val.Substring(sindx,sw.Length)
尺寸S1为size = TextRenderer.MeasureText(e.Graphics,sBefore,e.CellStyle.Font,e.CellBounds.Size)
尺寸S2作为大小= TextRenderer.MeasureText(e.Graphics,剑,e.CellStyle.Font,e.CellBounds.Size)

'调整宽度,使的HIGHlite更准确
。如果s1.Width > 5然后
hl_rect.X = e.CellBounds.X + s1.Width - 5
hl_rect.Width = s2.Width - 6
,否则
hl_rect.X = e.CellBounds .X + 2
hl_rect.Width = s2.Width - 6
端如果

'使用时,选择行
昏暗hl_brush作为SolidBrush $ b暗亮点$ b。如果((e.State而DataGridViewElementStates.Selected)LT;> DataGridViewElementStates.None)然后
hl_brush =新SolidBrush(Color.DarkGoldenrod)
,否则
hl_brush =新SolidBrush(彩色.LightGoldenrodYellow)
端如果

'烤漆搜索词
e.Graphics.FillRectangle后面的背景(hl_brush,hl_rect)

hl_brush.Dispose ()
端如果
端如果

'绘制内容照常
e.PaintContent(e.CellBounds)
端如果
结束小组


解决方案

我不认为有任何内置的这样做的方式,但我认为你可以处理 DataGridView的 CellPainting 事件,设置 e.Handled = TRUE; ,然后自己绘制它,你需要它



您可能能够使用 PaintBackground 来减少你必须做你自己的工作量。


How can i Highlight Part of a text in a cell of datagridview ? I am using C#.
For example user searches book. on of cells contains bookmark. I want to highlight "book" in bookmark. Thanks.


Edition. 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

解决方案

I don't think there's any built in way of doing it, but I'd assume that you could handle the CellPainting event of the DataGridView, set e.Handled = true; and then draw it yourself as you need it.

You might be able to use PaintBackground to minimize the amount of work you have to do yourself.

这篇关于突出显示文本的一部分在DataGridView中的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆