• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 800
  • Last Modified:

Overriding OnPaint DataGrid event

Using .NEt 1.1, Windows Forms, VB.NET

I have a datagrid that has a dataTable behind it. Depending on the column value in a specific row, I want to color the entire row red. Then if the user clicks anywhere on this row, I want to change it to blue.

Any ideas on how I could do this using the onPaint event of the dataGrid?

1 Solution
Bob LearnedCommented:
Here is an example of defining a colored TextBox column:

Public Class DataGridColorTextBoxColumn
  Inherits DataGridTextBoxColumn

  Private m_maxColumn As DataGridColorTextBoxColumn
  Public Property MaxValueColumn() As DataGridColorTextBoxColumn
      Return m_maxColumn
    End Get
    Set(ByVal Value As DataGridColorTextBoxColumn)
      m_maxColumn = Value
    End Set
  End Property

  Private m_source As CurrencyManager

  Private Function GetRowValue(ByVal rowNumber As Integer) As String
    Return Me.GetColumnValueAtRow(m_source, rowNumber)
  End Function

  Protected Overloads Overrides Sub Paint(ByVal g As Graphics, _
   ByVal bounds As Rectangle, ByVal source As CurrencyManager, _
   ByVal rowNum As Integer, ByVal backBrush As Brush, ByVal foreBrush As Brush, ByVal alignToRight As Boolean)


      If Me.MaxValueColumn Is Nothing Then
        Throw New ArgumentException("Max value column is not set.")
      End If

      m_source = source

      Dim columnValue As Double = Me.GetRowValue(rowNum)

      If columnValue > Me.MaxValueColumn.GetRowValue(rowNum) Then
        backBrush = New SolidBrush(Color.Green)
        foreBrush = New SolidBrush(Color.White)
      End If

    Catch ex As Exception



      MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight)

    End Try

  End Sub

End Class

You might be able to extend it to color an entire row.

SanmarieAuthor Commented:
Thanks Bob,

I knew there was a way to color each cell of the dataGrid by inheriting from the DataGridTextBoxColumn class but based on your solution there is no way to manipulate an entire row of columns by overriding the paint method of the DataGrid itself instead of the paint event of the Textbox. Correct?




Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now