Image in Datagrid Column (with Class from here)??

DTFan

New member
Joined
Apr 12, 2005
Messages
2
Programming Experience
Beginner
Hey everyone. Still totally new to .NET. I've been using for a little while but
still find myself banging my head against the wall on a pretty regular basis

Of course, that's nothing new so ...

Anyway, I'm playing around with Datagrids (trying to wrap my head around
the what's and why's). I'm working with a simple 2 column datagrid (one
textbox and one image (bitmap). I found several examples of people using
bitmaps in columns but wasn't really able to see exactly what they were
doing. Then I found this class (from one of these forums) which made it look
very easy. Just pre-load the images into a ImageList and then pass that to
the class.

I'm including the class and the two procedures from the calling form. My
problem is that the column is showing up (proper width, heading, etc.), but
the image is not there. I look into the ImageList properties, at Collection, and
the 3 images that I have in my bin directory (which are the ones I added) are
there but they're not showing up in the Datagrid column.

Can anyone see where I'm going wrong?

VB.NET:
[font=Courier New][color=#006400]'*** Two Procedures *** [/color]
	[color=blue]Private[/color] [color=blue]Sub[/color] MakeDataSet()
 
		[color=blue]Dim[/color] tTestImage [color=blue]As[/color] DataTable
 
		myDataSet = [color=blue]New[/color] DataSet("myDataSet")
		tTestImage = [color=blue]New[/color] DataTable("Test")
 
		[color=blue]Dim[/color] cLIName [color=blue]As[/color] DataColumn
		[color=blue]Dim[/color] cLIImage [color=blue]As[/color] DataColumn
 
		cLIName = [color=blue]New[/color] DataColumn("liName")
		cLIImage = [color=blue]New[/color] DataColumn("liImage")
 
		tTestImage.Columns.Add(cLIName)
		tTestImage.Columns.Add(cLIImage)
 
		myDataSet.Tables.Add(tTestImage)
 
	[color=blue]End[/color] [color=blue]Sub[/color]
[/font]
 
[font=Courier New]	[color=blue]Private[/color] [color=blue]Sub[/color] AddCustomDataTableStyle()
 
		[color=blue]Dim[/color] ts1 [color=blue]As[/color] DataGridTableStyle
 
		ts1 = [color=blue]New[/color] DataGridTableStyle
		ts1.MappingName = "Test"
		ts1.AlternatingBackColor = Color.LightGray
 
		[color=blue]Dim[/color] txtLIName [color=blue]As[/color] DataGridTextBoxColumn
 
		txtLIName = [color=blue]New[/color] DataGridTextBoxColumn
 
		[color=blue]With[/color] txtLIName
			.Alignment = HorizontalAlignment.Center
			.MappingName = "liName"
			.HeaderText = "Name"
			.Width = 85
		[color=blue]End[/color] [color=blue]With[/color]
 
		ts1.GridColumnStyles.Add(txtLIName)
 
		[color=blue]Dim[/color] ImageCol [color=blue]As[/color] [color=blue]New[/color] ImageColumnStyle [color=darkgreen]'<-- Name of class[/color]
 
		[color=blue]With[/color] ImageCol
			.ImageList = ImageList1 [color=darkgreen]'<-- My imagelist object[/color]
			.BackColor = Brushes.White
			.ForeColor = Brushes.Black
			.RightToLeft = [color=blue]True[/color]
			.MappingName = "liImage"
			.HeaderText = "Image"
			.Width = 75
			.Alignment = HorizontalAlignment.Center
			.ReadOnly = [color=blue]True[/color]
		[color=blue]End[/color] [color=blue]With[/color]
 
		ts1.GridColumnStyles.Add(ImageCol)
 
		dataGrid1.TableStyles.Add(ts1)
 
	[color=blue]End[/color] [color=blue]Sub[/color]
 
 
[font=Courier New][color=#006400]'*** Class ***[/color]
 
	Imports System.Drawing
	Imports System
	Imports System.Windows.Forms
 
	[color=blue]Public[/color] Class ImageColumnStyle
 
		Inherits DataGridColumnStyle
		[color=blue]Public[/color] ImageList [color=blue]As[/color] ImageList
		[color=blue]Private[/color] Image [color=blue]As[/color] Image
		[color=blue]Private[/color] bBackcolor [color=blue]As[/color] Brush
		[color=blue]Private[/color] bForeColor [color=blue]As[/color] Brush
		[color=blue]Private[/color] bRightToLeft [color=blue]As Boolean[/color]
 
		Protected Overrides [color=blue]Sub[/color] Abort([color=blue]ByVal[/color] rowNum [color=blue]As Integer[/color])
 
			Invalidate()
 
		[color=blue]End[/color] [color=blue]Sub[/color]
 
		Protected Overrides [color=blue]Function[/color] Commit( _
[color=blue][color=#000000]			[/color]ByVal[/color] dataSource [color=blue]As[/color] System.Windows.Forms.CurrencyManager, _
			[color=blue]ByVal[/color] rowNum [color=blue]As Integer[/color]) [color=blue]As Boolean[/color]
 
			[color=blue]Return[/color] [color=blue]True[/color]
 
		[color=blue]End[/color] [color=blue]Function[/color]
 
		Protected Overloads Overrides [color=blue]Sub[/color] Edit( _
			[color=blue]ByVal[/color] source [color=blue]As[/color] System.Windows.Forms.CurrencyManager, _
			[color=blue]ByVal[/color] rowNum [color=blue]As Integer[/color], [color=blue]ByVal[/color] bounds [color=blue]As[/color] System.Drawing.Rectangle, _
			[color=blue]ByVal[/color] [readOnly] [color=blue]As Boolean[/color], _
			[color=blue]ByVal[/color] instantText [color=blue]As String[/color], _
			[color=blue]ByVal[/color] cellIsVisible [color=blue]As Boolean[/color])
 
		[color=blue]End[/color] [color=blue]Sub[/color]
 
		Protected Overrides [color=blue]Function[/color] GetMinimumHeight() [color=blue]As Integer[/color]
 
			[color=blue]Return[/color] ImageList.ImageSize.Height
 
		[color=blue]End[/color] [color=blue]Function[/color]
 
		Protected Overrides [color=blue]Function[/color] GetPreferredHeight([color=blue]ByVal[/color] g [color=blue]As[/color] System.Drawing.Graphics, _
			[color=blue]ByVal[/color] value [color=blue]As[/color] [color=blue]Object[/color]) [color=blue]As Integer[/color]
 
			[color=blue]Return[/color] ImageList.ImageSize.Height
 
		[color=blue]End[/color] [color=blue]Function[/color]
 
		Protected Overrides [color=blue]Function[/color] GetPreferredSize([color=blue]ByVal[/color] g [color=blue]As[/color] System.Drawing.Graphics, _
			[color=blue]ByVal[/color] value [color=blue]As[/color] [color=blue]Object[/color]) [color=blue]As[/color] System.Drawing.Size
 
			[color=blue]Return[/color] ImageList.ImageSize
 
		[color=blue]End[/color] [color=blue]Function[/color]
 
		Protected Overloads Overrides [color=blue]Sub[/color] Paint([color=blue]ByVal[/color] g [color=blue]As[/color] System.Drawing.Graphics, _
			[color=blue]ByVal[/color] bounds [color=blue]As[/color] System.Drawing.Rectangle, _
[color=blue]ByVal[/color] source [color=blue]As[/color] System.Windows.Forms.CurrencyManager, _
			[color=blue]ByVal[/color] rowNum [color=blue]As Integer[/color])
 
			Paint(g, bounds, source, rowNum, BackColor, ForeColor, RightToLeft)
 
		[color=blue]End[/color] [color=blue]Sub[/color]
 
		Protected Overloads Overrides [color=blue]Sub[/color] Paint([color=blue]ByVal[/color] g [color=blue]As[/color] System.Drawing.Graphics, _
			[color=blue]ByVal[/color] bounds [color=blue]As[/color] System.Drawing.Rectangle, _
			[color=blue]ByVal[/color] source [color=blue]As[/color] System.Windows.Forms.CurrencyManager, _
			[color=blue]ByVal[/color] rowNum [color=blue]As Integer[/color], [color=blue]ByVal[/color] alignToRight [color=blue]As Boolean[/color])
 
		[color=blue]End[/color] [color=blue]Sub[/color]
 
		Protected Overloads Overrides [color=blue]Sub[/color] Paint([color=blue]ByVal[/color] g [color=blue]As[/color] System.Drawing.Graphics, _
			[color=blue]ByVal[/color] bounds [color=blue]As[/color] System.Drawing.Rectangle, _
			[color=blue]ByVal[/color] source [color=blue]As[/color] System.Windows.Forms.CurrencyManager, _
			[color=blue]ByVal[/color] rowNum [color=blue]As Integer[/color], [color=blue]ByVal[/color] BackBrush [color=blue]As[/color] Brush, _
			[color=blue]ByVal[/color] ForeBrush [color=blue]As[/color] Brush, [color=blue]ByVal[/color] alignToRight [color=blue]As Boolean[/color])
 
			[color=blue]Dim[/color] index = GetColumnValueAtRow(source, rowNum)
 
			[color=blue]If[/color] [color=blue]Not[/color] IsDBNull(index) [color=blue]Then[/color]
				InitImage(index)
 
				[color=blue]Dim[/color] rect [color=blue]As[/color] Rectangle = [color=blue]New[/color] Rectangle(bounds.Location, ImageList.ImageSize)						
 
				g.FillRectangle(BackBrush, bounds)
				g.DrawImage(Image, rect)
			[color=blue]End If[/color]
 
		[color=blue]End[/color] [color=blue]Sub[/color]
 
 
[/font][font=Courier New]		[color=blue]Private[/color] [color=blue]Sub[/color] InitImage([color=blue]ByVal[/color] index [color=blue]As Integer[/color])
 
			[color=blue]If[/color] [color=blue]Not[/color] IsDBNull(ImageList) [color=blue]And[/color] ImageList.Images.Count >= index + 1 [color=blue]Then[/color]
				Image = ImageList.Images(index)
			[color=blue]End If[/color]
 
		[color=blue]End[/color] [color=blue]Sub[/color]
 
		Property BackColor() [color=blue]As[/color] Brush
 
			[color=blue]Get[/color]
				[color=blue]Return[/color] BackColor
			[color=blue]End[/color] [color=blue]Get[/color]
 
			[color=blue]Set[/color]([color=blue]ByVal[/color] Value [color=blue]As[/color] Brush)
				bBackcolor = Value
			[color=blue]End[/color] [color=blue]Set[/color]
 
		[color=blue]End Property[/color]
 
		Property ForeColor() [color=blue]As[/color] Brush
 
			[color=blue]Get[/color]
				[color=blue]Return[/color] ForeColor
			[color=blue]End[/color] [color=blue]Get[/color]
 
			[color=blue]Set[/color]([color=blue]ByVal[/color] Value [color=blue]As[/color] Brush)
				bForeColor = Value
			[color=blue]End[/color] [color=blue]Set[/color]
 
		[color=blue]End Property[/color]
 
		Property RightToLeft() [color=blue]As Boolean[/color]
 
			[color=blue]Get[/color]
				[color=blue]Return[/color] RightToLeft
			[color=blue]End[/color] [color=blue]Get[/color]
 
			[color=blue]Set[/color]([color=blue]ByVal[/color] Value [color=blue]As Boolean[/color])
				bRightToLeft = Value
			[color=blue]End[/color] [color=blue]Set[/color]
 
		[color=blue]End Property[/color]
 
	[color=blue]End[/color] Class[/font] 
 
[/font]
So, like I was saying, the datagrid pops up (nothing in at default) but the
image is not in the column. I'm pretty sure the ImageList is correctly
formatted although I don't know how to check if the images are there (but I
am adding them through the Collections Property so I'm assuming they are).

Any advice and/or tips/hints? I thought about possibly using an array of
bitmaps but wasn't sure if it would work with this class.

Any and all help is greatly appreciated.

Thanks in advance.
 
Top Bottom