DataGrid MouseOver - change background color?

  • Thread starter Thread starter James Radke
  • Start date Start date
J

James Radke

Hello,

I was wondering, is it possible in a windows form datagrid to set up a
mouseover / mouseout style event where when you hold the mouse over a row,
it changes the background color of the entire row (i.e. highlights it), and
then when you move the mouse away from that row, the background color
changes back to the original color?

That way, if you have a datagrid the user can use the mouse to easily line
up the columns, etc?

Can anyone help me as to how I can do this, if it is possible?

Thanks!

Jim
 
Hi,

Here is a datagridtextboxcolumn what i have been working. Maybe
it will help.

Public Class HyperLinkColumn

Inherits DataGridTextBoxColumn

Dim mstrLink As String

Dim rectPaint As New RectangleF

Dim fnt As New Font(MyBase.TextBox.Font.Name, MyBase.TextBox.Font.Size,
FontStyle.Underline)

Dim curOld As Cursor

Dim WithEvents dg As DataGrid

Dim oldCell As New Point(-1, -1)



Public Sub HandleMouseMove(ByVal sender As Object, ByVal e As
MouseEventArgs) Handles dg.MouseMove

Dim g As Graphics = dg.CreateGraphics

Dim bounds As Rectangle

Dim hti As DataGrid.HitTestInfo = dg.HitTest(New Point(e.X, e.Y))

Dim isInCell As Boolean = (hti.Row > -1 And hti.Column = 2)

Static bRedraw As Boolean = False

If isInCell Then

Dim pt As Point

pt = New Point(hti.Row, hti.Column)

If Not pt.Equals(oldCell) Then

'

' Create a region where we want the datagrid to redraw

' So the datagrid doesn't flash.

'

Dim pthToRedraw As New Drawing2D.GraphicsPath

Dim rgnToRedraw As Region

Dim rCell As Rectangle = dg.GetCellBounds(pt.X, pt.Y)

pthToRedraw.AddRectangle(rCell)

If oldCell.X > -1 Then

'

' Have to redraw last cell

'

pthToRedraw.AddRectangle(dg.GetCellBounds(oldCell.X, oldCell.Y))

End If

rgnToRedraw = New Region(pthToRedraw)

dg.Invalidate(rgnToRedraw)

End If

'

' Flag datagrid for redraw

'

bRedraw = True

oldCell = pt

Else

'

' Only redraw when needed

'

If bRedraw Then

dg.Invalidate(dg.GetCellBounds(oldCell.X, oldCell.Y))

End If

bRedraw = False

oldCell = New Point(-1, -1)

End If

End Sub

Protected Overloads Overrides Sub Edit(ByVal source As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds
As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal instantText
As String, ByVal cellIsVisible As Boolean)

Application.DoEvents()

Process.Start(mstrLink)

End Sub

Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing.Graphics,
ByVal bounds As System.Drawing.Rectangle, ByVal source As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal
backBrush As System.Drawing.Brush, ByVal foreBrush As System.Drawing.Brush,
ByVal alignToRight As Boolean)

Static bPainted As Boolean = False

'

' First time we paint get a reference to datagrid

' So we can consume its events

'

If Not bPainted Then dg = Me.DataGridTableStyle.DataGrid

Dim pt As New Point(rowNum, 2)

bPainted = True

'clear the cell

If pt.Equals(oldCell) Then

bounds.Inflate(-1, -1)

g.FillRectangle(Brushes.LightBlue, bounds)

g.DrawRectangle(Pens.Blue, bounds)

Else

g.FillRectangle(backBrush, bounds)

End If

'draw the value

Dim s As String = Me.GetColumnValueAtRow([source], rowNum).ToString()

Dim sf As New StringFormat

sf.LineAlignment = StringAlignment.Center

Dim r As New RectangleF(bounds.X, bounds.Y, bounds.Width, bounds.Height)

g.DrawString(s, fnt, foreBrush, r, sf)

mstrLink = s

rectPaint = r

End Sub

End Class



Ken

--------------------------
 
Back
Top