突出显示datagridview单元格中文本的一部分
如何突出显示datagridview单元格中文本的一部分? 我正在使用C#。
例如用户搜索书。单元格上包含书签。我想在书签中突出显示“书”。 谢谢。突出显示datagridview单元格中文本的一部分
版。 这个代码可以使用吗?
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
事件DataGridView
,集e.Handled = true;
然后自己绘制它,你需要它。
您可能可以使用PaintBackground
来最大限度地减少自己的工作量。
我也在寻找一种方法来做到这一点。进入明显的CellPainting事件后,我发现使用事件的Graphics对象没有办法。但是,我确实设法使用DataGridView.GetGraphics()方法中的Graphics对象来突出显示部分文本。 我假设你已经知道如何找到包含你搜索的字符串的单元格。 的CellPainting事件中你要做的第一件事是画的细胞的任何其他细胞:
e.Paint(e.ClipBounds, DataGridViewPaintParts.All);
接下来要做的是将单元格的文本分成2份 - 搜索文本之前的部分和搜索文本本身。你需要这个来计算你想要突出显示的矩形。
然后使用Graphics对象的MeasureString方法来获取单元格内搜索文本的位置。由于我使用的是与网格本身相关的图形对象,而不是事件的图形对象,因此我必须计算网格内高亮矩形的位置。我使用DataGridView.GetCellDisplayRectangle方法来找到网格内的单元格的位置,并增加了亮点矩形位置的这个位置上它只是使用的的FillRectangle和束带
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);
从这一点图形对象:
g.FillRectangle(new SolidBrush(Color.Black), HighLightedRect);
g.DrawString(HighlighetText, dgvGrid.Font, new SolidBrush(Color.White), HighLightedRect.Location);
g.Flush();
,当然还有,完成后不要忘记设置E要真正的Handled属性:
哦,的一个L ast thing:您需要使整个网格失效,或者每次搜索新字符串时至少使上一次搜索中突出显示的单元格失效,否则最终会显示一个网格,其中包含与该网格无关的突出显示的文本当前搜索字符串。
按部分你的意思是插入文本的一部分,整个文本或什么? – 2010-08-10 10:36:06
文本的一部分。 例如用户搜索的书。单元格上包含书签。 我想在书签中突出显示“book”。 – Shahin 2010-08-10 10:37:59