Skip to content

Add Image Column to DataGridView with Enum Support

I stumbled upon a great article which describes how to create an inherited DataGridViewImageCell and inherited DataGridViewColumn control to achieve an image per row in your DataGridView.

In summary here is the custom control you will need, place in a DataGridViewCustomCell.cs or the like in your UI app or class library:

    namespace DataGridViewCustomCell
    {
        ///
        /// Available status images.
        ///
        public enum StatusImage
        {
            Green,
            Yellow,
            Red
        }

        ///
        /// Column which will be attached to the DataGridView.
        ///
        public class ImageStatusColumn : DataGridViewColumn
        {
            public ImageStatusColumn()
                : base(new StatusCell())
            {
            }
            private StatusImage m_DefaultStatus = StatusImage.Red;

            public StatusImage DefaultStatus
            {
                get { return m_DefaultStatus; }
                set { m_DefaultStatus = value; }
            }

            public override object Clone()
            {
                ImageStatusColumn col = base.Clone() as ImageStatusColumn;
                col.DefaultStatus = m_DefaultStatus;
                return col;
            }

            public override DataGridViewCell CellTemplate
            {
                get { return base.CellTemplate; }
                set
                {
                    if ((value == null) || !(value is StatusCell))
                    {
                        throw new ArgumentException(
                            "Invalid cell type, StatusColumns can only contain StatusCells");
                    }
                }
            }
        }

        ///
        /// Cell which is attached to the ImageStatusColumn.
        ///
        public class StatusCell : DataGridViewImageCell
        {
            public StatusCell()
            {
                this.ImageLayout = DataGridViewImageCellLayout.Zoom;
            }

            protected override object GetFormattedValue(object value,
               int rowIndex, ref DataGridViewCellStyle cellStyle,
               TypeConverter valueTypeConverter,
               TypeConverter formattedValueTypeConverter,
               DataGridViewDataErrorContexts context)
            {

                Bitmap resource = Properties.Resources.bullet_ball_red;
                StatusImage status = StatusImage.Red;
                // Try to get the default value from the containing column
                ImageStatusColumn owningCol = OwningColumn as ImageStatusColumn;
                if (owningCol != null)
                {
                    status = owningCol.DefaultStatus;
                }
                if (value is StatusImage || value is int)
                {
                    status = (StatusImage)value;
                }
                switch (status)
                {
                    case StatusImage.Green:
                        resource = Properties.Resources.bullet_ball_green;
                        break;
                    case StatusImage.Yellow:
                        resource = Properties.Resources.bullet_ball_yellow;
                        break;
                    case StatusImage.Red:
                        resource = Properties.Resources.bullet_ball_red;
                        break;
                    default:
                        break;
                }
                cellStyle.Alignment = DataGridViewContentAlignment.TopCenter;
                return resource;
            }
        }

    }

All you then need to do is rename the resources I have listed above to match ones of your own and set your ImageStatusColumn like so:

It seems you can typically get an image in a DataGridView if you are happy to keep the same image for the entire column, but this custom control allows for you to set the image you require by selecting an enum. This could no doubt be easily extended to take an image path.

Results in:

Nothing to it!

For more information see: http://www.informit.com/articles/article.aspx?p=446453&seqNum=14

VN:F [1.9.10_1130]
Rating: 5.0/5 (3 votes cast)
VN:F [1.9.10_1130]
Rating: +1 (from 1 vote)
Add Image Column to DataGridView with Enum Support, 5.0 out of 5 based on 3 ratings
Bookmark and Share
kick it on DotNetKicks.com
Shout it

NOW, FOR A WORD FROM OUR SPONSORS

2 Comments

  1. NICE…………

    VA:F [1.9.10_1130]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.10_1130]
    Rating: 0 (from 0 votes)
    Posted on 12-May-09 at 9:16 pm | Permalink
  2. Bob

    Uhh…where is the link to the “great article”? Putz.

    VA:F [1.9.10_1130]
    Rating: 5.0/5 (1 vote cast)
    VA:F [1.9.10_1130]
    Rating: 0 (from 0 votes)
    Posted on 08-Sep-11 at 12:00 am | Permalink

One Trackback/Pingback

  1. DotNetShoutout on 10-Feb-09 at 12:08 pm

    Add Image Column to DataGridView with Enum Support…

    Thank you for submitting this cool story – Trackback from DotNetShoutout…

    VA:F [1.9.10_1130]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.10_1130]
    Rating: 0 (from 0 votes)

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*

My name is Graham O'Neale and I'm a software architect from Gold Coast, Australia. I am an overtime thinker, full time coder and awake part time in the real world. I have a keen interest in software development, particularly in the realm of programming (C#, ASP.NET, ASP.NET MVC, LINQ (2 SQL), Entity Framework, Silverlight, Blend, WCF, WPF) and a keen interest in the cutting edge and innovation. I have a new found love for design patterns, ALT.NET practices and well crafted software architecture. The purpose of this blog is to express any thoughts, findings, tips and gripes along my travels in the wonderful world of coding and technology...