突出显示 datagridview 单元格中的部分文本
如何突出显示 datagridview 单元格中的部分文本? 我正在使用 C#。
例如用户搜索书籍。的单元格包含书签。我想在书签中突出显示“书”。 谢谢。
版。 这段代码可以吗?
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不认为有任何内置的方法可以做到这一点,但我假设您可以处理
DataGridView
的CellPainting
事件,设置e. Handled = true;
然后根据需要自己绘制。您也许可以使用
PaintBackground
来最大程度地减少您必须自己完成的工作量。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 theDataGridView
, sete.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.我也在寻找一种方法来做到这一点。在进行了明显的 CellPainting 事件之后,我发现使用该事件的 Graphics 对象并不能解决问题。但是,我确实设法使用 DataGridView.GetGraphics() 方法中的 Graphics 对象来突出显示文本的一部分。
我假设您已经知道如何找到包含您搜索的字符串的单元格。
在 CellPainting 事件中,您要做的第一件事就是像任何其他单元格一样绘制单元格:
接下来要做的是将单元格的文本分为两部分 - 搜索文本之前的部分和搜索文本本身。您需要它来计算要突出显示的矩形。
然后使用 Graphics 对象的 MeasureString 方法获取单元格内搜索文本的位置。由于我使用的是与网格本身相关的 Graphics 对象,而不是事件的 Graphics 对象,因此我必须计算网格内突出显示矩形的位置。我已经使用 DataGridView.GetCellDisplayRectangle 方法来查找网格内单元格的位置,并添加了突出显示矩形位置的位置:
从那时起,它只需使用 Graphics 对象的 FillRectangle 和 DrawString:
当然,完成后,不要忘记将 e 的 Handled 属性设置为 true:
哦,还有最后一件事:每次搜索新的网格时,您都需要使整个网格无效,或者至少使先前搜索中突出显示的单元格无效。字符串,否则您最终会得到一个充满突出显示文本的网格,这些文本与当前搜索字符串无关。
I too was searching for a way to do that. After going to the obvious CellPainting event I've found that using the event's Graphics object did not do the trick. However, I did manage to use the Graphics object from the DataGridView.GetGraphics() method to do highlight a part of the text.
I assume you already know how to find the cell that contains the string you search.
inside the CellPainting event the first thing you want to do is paint the cell as any other cell:
The next thing to do is to split the cell's text to 2 parts - the part before the search text and the search text itself. you need this in order to calculate the rectangle where you want to highlight.
Then use the MeasureString method of the Graphics object to get the location of the search text within the cell. Since I'm using the Graphics object related to the grid itself, and not the Graphics object of the event, I had to calculate the location of the highlight rectangle within the grid. I've used the DataGridView.GetCellDisplayRectangle method to find the location of the cell inside the grid, and added this location of the highlight rectangle location:
From that point on it's simply using the FillRectangle and DrawString of the Graphics object:
and of course, don't forget to set the Handled property of e to true when done:
Oh, and one last thing: You will need to invalidate the entire grid, or at least the cells that was highlighted in the previous search every time you search a new string, otherwise you will end up with a grid full of highlighted text that has nothing to do with the current search string.