image column in datagrid for windows forms

diyagp

Member
Joined
Jan 20, 2005
Messages
5
Programming Experience
1-3
how do i add an Image column in the datagrid for windows forms
 
C# sample

here is a source code in C#. I suppose it's no problem to recode it to VB. Its simplified and has lots of bugs, but it works.
You have ot create ImageList object and set the public static imageList reference of the class prior to using the class.
And, of course, you have to define table style for datagrid manually in code. You cann't use this class in designer.
Value in column mapped to this column style determines index of used image in imageList.

using System;
using System.Windows.Forms;
using System.Drawing;

namespace ImageColumnStyleTest
{
/// <summary>
/// Summary description for ImageColumnStyle.
/// </summary>
public class ImageColumnStyle : DataGridColumnStyle
{
private Image image = null;
public static ImageList imageList = null;

public ImageColumnStyle() : base()
{
}

protected override void Abort(int rowNum)
{
Invalidate();
}

protected override void Edit(CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
{
}

protected override bool Commit(CurrencyManager dataSource, int rowNum)
{
return true;
}

protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, CurrencyManager source, int rowNum)
{
Paint(g, bounds, source, rowNum, false);
}

protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, CurrencyManager source, int rowNum, bool alignToRight)
{
Paint(
g,
bounds,
source,
rowNum,
Brushes.Red,
Brushes.Blue,
alignToRight);
}

protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
{
int index = (int)base.GetColumnValueAtRow(source, rowNum);
InitImage(index);

Rectangle rect = new Rectangle(bounds.Location, imageList.ImageSize);
g.FillRectangle(backBrush, bounds);
g.DrawImage(image, rect);
}

protected override int GetPreferredHeight(Graphics g, object value)
{
return imageList.ImageSize.Height;
}

protected override int GetMinimumHeight()
{
return imageList.ImageSize.Height;
}

protected override Size GetPreferredSize(Graphics g, object value)
{
return imageList.ImageSize;
}

private void InitImage(int index)
{
if(imageList != null && imageList.Images.Count >= index + 1)
{
image = imageList.Images[index];
}
}
}
}
 
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 : :cool:

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 :D
 
set a column's visibility property to false

well thx aviva, for the code

Can someone tell me if there is way where i can set a datagrid's column's visibility property to false. As this column will contain the primary key values.

though it's visibility is set to false i should be able to access the column's values. The datasource for the datagrid is a datatable.

thx in adv
 
Hi.


You can try to create custom columns Style, lets say ... 4 columns and in the table set create an additional column (5th) and make him the KEY.

Avia
 
icon in datagrid

Hi...I have a table which has an image column. It essentially contains small round images. How do I show these icons in the datagrid since I dont have an imagelist?
 
well thx aviva, for the code

Can someone tell me if there is way where i can set a datagrid's column's visibility property to false. As this column will contain the primary key values.

though it's visibility is set to false i should be able to access the column's values. The datasource for the datagrid is a datatable.

thx in adv

hi you can use this code sample to hide datagrid columns


Sub HideColumnOfDataSet()
Dim points As New System.Data.DataTable("Points")
points.Columns.Add(New DataColumn("X", GetType(Integer)))
points.Columns.Add(New DataColumn("Y", GetType(Integer)))
points.Rows.Add(New Object() {1, 2})
points.Rows.Add(New Object() {3, 5})
DataGrid1.DataSource = points
Dim tableStyle As New DataGridTableStyle
tableStyle.MappingName = "Points"
DataGrid1.TableStyles.Add(tableStyle)
DataGrid1.TableStyles("Points").GridColumnStyles("X").Width = 0
End Sub
 
hi could some one help me with the same problem which has been posted earlier its like even i have a table with an image column i wanna know how to display it in a datagrid
 
hi

i added the image code and it all went well but im not sure how to add the pic the datagrid.

in the datagrid i added a new colum and then i tried to add the pic using this code.

Prodgrid.Item(1, 5) = ImageList1.Images.Item(1)

their are images in the image list (5 of them) so this should work right.

whe i started the prog i got text in the colum instead "System.Drawing.Bitmap"

i could only think that changing the feild type in the dataset editer could solve this so i wnet their but their wasnt a object type or anythign that i thought could be relevent to this.

am i missing something

thanks
 
.Net 2.0 has DataGridView control which has built in support for image column, you should use this.
 
Image Column overlaps Text Column in Datagrid

Hi

Iam using VB.NET 2003 with Access Databse to retrieve images and display it in the grid.My Problem Image column overlaps Text Column.

Could any one help me.....It is very Urgent...............
 
Back
Top