vb.net 仅更改 DataGridView 单元格内部分文本的颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23420984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:30:12  来源:igfitidea点击:

Change color of only part of text inside a DataGridView cell

vb.netwinformsdatagridviewdatagridviewcolumn

提问by mcbalaji

enter image description here

在此处输入图片说明

I have a DGV with 3 columns and each columns contains some text i want to change the fore color of particular words in each columns.

我有一个有 3 列的 DGV,每列包含一些文本,我想更改每列中特定单词的前景色。

In column1a word contains _should be in greencolor, a word contains +should be in Redcolor,

column1一个字包含_应在green颜色,一个字包含+应在Red颜色,

for column2 if a word contain -it should be in purplecolor,

对于 column2,如果一个单词包含-它应该是purple彩色的,

for column3 if a word contains _the fore color should be Blue.

对于 column3 如果一个单词包含_前景色应该是Blue.

VB.NET datagridview windows form

VB.NET datagridview windows窗体

回答by SSS

Modified from Microsoft help article

修改自微软帮助文章

Imports System.Data

Public Class Form1
  Dim mdtbColourMap As DataTable = Nothing

  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    '----Following should be replaced with your data access
    With DataGridView1
      .Columns.Add("Column1", "Column1")
      .Columns.Add("Column2", "Column2")
      .Columns.Add("Column3", "Column3")
      .Rows.Add("Welcome to_ the", "Wonderful-", "World of computing_")
      .Rows.Add("This_ is what+ I", "Want-", "In My_ laptop")
      .Rows.Add("X_is always+", "Greater-", "Than_ y")
      .Columns(0).Width = 120
      .Columns(1).Width = 120
      .Columns(2).Width = 120
    End With
    '----Above should be replaced with your data access

    'Define the search terms and color for each
    mdtbColourMap = New DataTable
    mdtbColourMap.Columns.Add(New DataColumn("SearchTerm", GetType(String)))
    mdtbColourMap.Columns.Add(New DataColumn("TextColor", GetType(Brush)))
    mdtbColourMap.Rows.Add("_", Drawing.Brushes.Green)
    mdtbColourMap.Rows.Add("+", Drawing.Brushes.Red)
    mdtbColourMap.Rows.Add("-", Drawing.Brushes.Purple)


  End Sub


  Private Sub DataGridView1_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting

    If e.ColumnIndex >= 0 And e.RowIndex >= 0 Then
      Dim newRect As New Rectangle(e.CellBounds.X + 1, e.CellBounds.Y + 1, _
          e.CellBounds.Width - 4, e.CellBounds.Height - 4)
      Dim backColorBrush As New SolidBrush(e.CellStyle.BackColor)
      Dim gridBrush As New SolidBrush(Me.DataGridView1.GridColor)
      Dim gridLinePen As New Pen(gridBrush)
      Try
        ' Erase the cell.
        e.Graphics.FillRectangle(backColorBrush, e.CellBounds)

        ' Draw the grid lines (only the right and bottom lines; 
        ' DataGridView takes care of the others).
        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, _
            e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, _
            e.CellBounds.Bottom - 1)
        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, _
            e.CellBounds.Top, e.CellBounds.Right - 1, _
            e.CellBounds.Bottom)

        ' Draw the inset highlight box.
        e.Graphics.DrawRectangle(Pens.Blue, newRect)

        ' Draw the text content of the cell, ignoring alignment. 
        If (e.Value IsNot Nothing) Then
          Dim strValue As String = CStr(e.Value)
          Dim strWords() As String = Split(strValue, " ")
          Dim strAlignment As String = "LEFT"
          If e.ColumnIndex = 0 Then strAlignment = "RIGHT"
          Dim sngX As Integer
          If strAlignment = "LEFT" Then
            sngX = e.CellBounds.X + 2
          Else
            sngX = e.CellBounds.Right - 4 - e.Graphics.MeasureString(strValue, e.CellStyle.Font).Width
          End If
          For i As Integer = 0 To strWords.GetUpperBound(0)
            Dim brsTextColor As Drawing.Brush = Nothing
            For j As Integer = 0 To mdtbColourMap.Rows.Count - 1
              Dim strSearchTerm As String = mdtbColourMap.Rows(j).Item("SearchTerm").ToString
              If InStr(strWords(i), strSearchTerm) > 0 Then
                brsTextColor = DirectCast(mdtbColourMap.Rows(j).Item("TextColor"), Drawing.Brush) 'change the color
                Exit For
              End If
            Next j
            If brsTextColor Is Nothing Then
              brsTextColor = Brushes.Black 'default
            End If
            e.Graphics.DrawString(strWords(i), e.CellStyle.Font, brsTextColor, sngX, e.CellBounds.Y + 2, StringFormat.GenericDefault)
            sngX += e.Graphics.MeasureString(strWords(i), e.CellStyle.Font).Width
          Next i
        End If
        e.Handled = True
      Finally
        gridLinePen.Dispose()
        gridBrush.Dispose()
        backColorBrush.Dispose()
      End Try

    End If

  End Sub

End Class