Hi,
You need to make your own columnstyle for that. Here is a column i
am working on that does that.
Code
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Imports System.Reflection
Imports System.ComponentModel
Public Class HotTrackTextBoxColumn
Inherits DataGridTextBoxColumn
Dim c As Integer
Dim rectPaint As New RectangleF
Dim fnt As New Font(MyBase.TextBox.Font.Name, MyBase.TextBox.Font.Size,
FontStyle.Underline)
Dim WithEvents dg As DataGrid
Dim oldCell As New Point(-1, -1)
Dim isInCell As Boolean = False
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))
isInCell = (hti.Row > -1 And hti.Column = c)
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 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
c = -1
For Each grdCol As DataGridColumnStyle In
Me.DataGridTableStyle.GridColumnStyles
c += 1
If grdCol.MappingName = Me.MappingName Then Exit For
Next
End If
bPainted = True
MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight)
Dim pnHot As New Pen(SystemColors.HotTrack)
Dim brHot As New SolidBrush(Color.FromArgb(128, SystemColors.HotTrack))
' see through brush
If MouseOverCell(rowNum) Then
bounds.Inflate(-1, -1)
g.FillRectangle(brHot, bounds)
g.DrawRectangle(pnHot, bounds)
End If
Dim r As New RectangleF(bounds.X, bounds.Y, bounds.Width, bounds.Height)
rectPaint = r
End Sub
Public ReadOnly Property MouseOverCell(ByVal rownum As Integer) As Boolean
Get
Dim pt As New Point(rownum, c)
Return pt.Equals(oldCell)
End Get
End Property
End Class
Link to example
http://www.onteorasoftware.com/Downloads/datagridcolumnstyles.zip
Ken
-----------------------
Hey all,
You notice how when you mouse over each thread in this forum how the
highlights, well how can I do this same thing in my win form with my
datagrid?
thanks,
rodchar