突出显示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 

我不认为有任何内置的方法,但我假设您可以处理DataGridViewCellPainting事件,设置e.Handled = true; 然后根据需要自己绘制。

您可以使用PaintBackground来最大限度地减少您自己必须完成的工作量。

我也在寻找一种方法来做到这一点。 在进入明显的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; 

哦,还有最后一件事:你需要使整个网格无效,或者至少每次搜索一个新字符串时在上一次搜索中突出显示的单元格,否则你最终会得到一个充满突出显示文本的网格。与当前搜索字符串无关。