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

Overriding OnPaint DataGrid event

Using .NEt 1.1, Windows Forms, VB.NET

Hello,
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?

Thanks
0
Sanmarie
Asked:
Sanmarie
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
    Get
      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)

    Try

      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

      MessageBox.Show(ex.ToString())

    Finally

      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.

Bob
0
 
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?

Thanks

San

0

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