Change color of only part of text inside a DataGridView cell

mcbalaji picture mcbalaji · May 2, 2014 · Viewed 7k times · Source

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.

In column1 a word contains _ should be in green color, a word contains +should be in Red color,

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

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

VB.NET datagridview windows form

Answer

SSS picture SSS · May 2, 2014

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