Overriding OnPaint DataGrid event

Posted on 2007-08-08
Last Modified: 2012-06-21
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?

Question by:Sanmarie
    LVL 96

    Accepted Solution

    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.


    Author Comment

    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?




    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now