lack of automated display in DataGridViewImageColumn

I found it a pity that you have to handle the CellFormatting event to map the value of a cell to an image in a DataGridViewImageColumn..
I think the DataGridViewImageColumn class should have a ImageList property that does this mapping automatically.
Or, if a direct mapping from a cell value to an ImageList index is not desired, some kind of indirection should be possible.


Answer this question

lack of automated display in DataGridViewImageColumn

  • Arjan Pot

    The class below inherits a DataGridGroupColorTextBoxColumn which I've written.  just replace it with DataGridTextBoxColumn.  As the class is a componant it has an ImageList on the designer which may need to be recreated.  Once you've sorted those issues out you should be cooking with gas.  In this example if the column value equals 3 then the image with ImageIndex 3 will be displayed.  It is a great solution to avoid storing images in a database and dragging them around the network.

    using System; 
    using System.IO;
    using System.Text;
    using System.Windows.Forms; 
    using System.Drawing; 

    public class DataGridLogIconColumn : DataGridGroupColorTextBoxColumn 
    {

    private System.Windows.Forms.ImageList LogImageList;
    private System.ComponentModel.IContainer components; 

    public DataGridLogIconColumn() 

    //base.New(); 
    InitializeComponent();
    base.ReadOnly = true; 
    }

    private void InitializeComponent()
    {
    this.components = new System.ComponentModel.Container();
    System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(DataGridLogIconColumn));
    this.LogImageList = new System.Windows.Forms.ImageList(this.components);
    // 
    // LogImageList
    // 
    this.LogImageList.ImageSize = new System.Drawing.Size(16, 16);
    this.LogImageList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("LogImageList.ImageStream")));
    this.LogImageList.TransparentColor = System.Drawing.Color.Transparent;



    protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight) 
    {

    Bitmap imgPic = new Bitmap(LogImageList.Images[Convert.ToInt32(GetColumnValueAtRow(source, rowNum))]);
    try 

    g.FillRectangle(base.GetBrush(rowNum, bounds), bounds.X, bounds.Y, bounds.Width, bounds.Height); 
    g.DrawImage(imgPic, bounds.X + ((bounds.Width - imgPic.Width) >> 1), bounds.Y, imgPic.Width, imgPic.Height); 

    catch 
    {


    }


  • Gary B.

    I am trying to figure out the image column.  

    This works:
    DataGridView1.CurrentCell.Value = System.Drawing.Image.FromFile("d:\test\folder.jpg")

    This doesn't:
    dsAlbums.Tables("tblAlbums").Rows.Add(dir, System.Drawing.Image.FromFile("D:\test\folder.jpg"))

    I put the image in an object in a dataset, which is the datasource for the datagridview.  Setting the column in question to a datagridviewimagecolumn gives me the following error:

    "System.FormatException the formatted value of the cell has the wrong type."

    ...anyone know why this is happening   Please help.

  • William Yeung

    Thanks Bloke,

    What I actually ended up doing here was creating this using the list view control instead of the datagrid I was originally working on.  I then customised the listview.drawitem event to get the exact layout I wanted.  I am also using the listview grouping functionality to further enhance the layout which lets me sort/group by any attribute or column very quickly using hashtables.  So far I am very impressed with the .net framework 2.0 and visual studio 2005 control enhancements, lots of very cool stuff in there.

  • lack of automated display in DataGridViewImageColumn