Image column in datagrid - vb.net
Hi.
I just finished converting th C# code to vb.net.
You need to do the following to work with this class :
1. you can create an imagelist on your form and asociate it in code ....
2. I add some more properties
Dim ts As New DataGridTableStyle
ts.MappingName = "ParentTable"
Dim ImageCol As New ImageColumnStyle
<-- Name of class
With ImageCol
.ImageList = ImageList1
<-- My imagelist object
.BackColor = Brushes.White
.ForeColor = Brushes.Black
.RightToLeft = True
.MappingName = "Icon"
.HeaderText = ""
.Width = ImageList1.ImageSize.Width + 4
.Alignment = HorizontalAlignment.Center
.ReadOnly = True
End With
ts.GridColumnStyles.Add(ImageCol)
DataGrid1.TableStyles.Add(ts)
The Class :
Imports System.Drawing
Imports System
Imports System.Windows.Forms
Public Class ImageColumnStyle
Inherits DataGridColumnStyle
Public ImageList As ImageList
Private Image As Image
Private bBackcolor As Brush
Private bForeColor As Brush
Private bRightToLeft As Boolean
Protected Overrides Sub Abort(ByVal rowNum As Integer)
Invalidate()
End Sub
Protected Overrides Function Commit(ByVal dataSource As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer) As Boolean
Return True
End Function
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)
End Sub
Protected Overrides Function GetMinimumHeight() As Integer
Return ImageList.ImageSize.Height
End Function
Protected Overrides Function GetPreferredHeight(ByVal g As System.Drawing.Graphics, ByVal value As Object) As Integer
Return ImageList.ImageSize.Height
End Function
Protected Overrides Function GetPreferredSize(ByVal g As System.Drawing.Graphics, ByVal value As Object) As System.Drawing.Size
Return ImageList.ImageSize
End Function
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)
Paint(g, bounds, source, rowNum, BackColor, ForeColor, RightToLeft)
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 alignToRight As Boolean)
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 Brush, ByVal ForeBrush As Brush, ByVal alignToRight As Boolean)
Dim index = GetColumnValueAtRow(source, rowNum)
If Not IsDBNull(index) Then
InitImage(index)
Dim rect As Rectangle = New Rectangle(bounds.Location, ImageList.ImageSize)
g.FillRectangle(BackBrush, bounds)
g.DrawImage(Image, rect)
End If
End Sub
Private Sub InitImage(ByVal index As Integer)
If Not IsDBNull(ImageList) And ImageList.Images.Count >= index + 1 Then
Image = ImageList.Images(index)
End If
End Sub
Property BackColor() As Brush
Get
Return BackColor
End Get
Set(ByVal Value As Brush)
bBackcolor = Value
End Set
End Property
Property ForeColor() As Brush
Get
Return ForeColor
End Get
Set(ByVal Value As Brush)
bForeColor = Value
End Set
End Property
Property RightToLeft() As Boolean
Get
Return RightToLeft
End Get
Set(ByVal Value As Boolean)
bRightToLeft = Value
End Set
End Property
End Class
Copy Paste it as a new class and have fun .
Avia