How to add images to DataGridView cell using C# and Windows Forms

There are many ways to skin this old cat. When choosing the way, you must first decide when should the adding of images take place. I chose to add images during sorting.

Let us assume you already added some data to gridview via datatable using ;

So now you know we have a filled up dataGridView.

Now let us assume in the dataGridView1 we want to add images according to the data within the 6th column. So what we first need to do is to create an image column by writing;

Now we have a image column. With the name “img” with the header “Image Column” As you can see in the 5th line I have stated that my images are actually icons. The reason I did this was that I found out that icons take up less space and flickers less. You can if you want change this option as you see fit.

Now let us get cracking and add those images according to the columns;

I hope this has been informative. Please do tell if you see any mistakes and wish to make suggestions.

Have fun!

This entry was posted in Coding for fun and tagged , , , , , , , , , on by .

About John Roach

My name is John Roach. I am an electrical-electronics engineer that has specialized in software development. This website contains very few snippets of my code written for fun and/or work. The rest, sadly, probably, will never make it to this site. You can view my projects from the link http://johnroach.info/projects . I have also written two papers on home automation; “A Load Balancing Protocol for IP Based Pervasive Networks” – John Sefik Roach; Corresponding Author: Ali Ziya Alkar; Smart Computing Review, vol. 2, no.2, October 2012 On page(s): 338-347; DOI: 10.6029/smartcr.2012.05.008 “IP Based Home Automation System” – Alkar, A.Z.; Roach, J. ; Baysal, D.; Consumer Electronics, IEEE Transactions on November 2010 Issue:4 On page(s): 2201 – 2207 ISSN: 0098-3063

20 thoughts on “How to add images to DataGridView cell using C# and Windows Forms

    1. John Roach Post author

      Oh that is the namespace of the program in Turkish it is a acronym for SÜT : Sipariş Ürün Teslim which means in English OPD: Order Product Delivery.
      I used this part of the code in another program. Forgot to rename it.

      Reply
    1. John Roach Post author

      I think you are talking of :
      6: Icon image = SUT.Properties.Resources.succcess_icon;
      7: dataGridView1.Rows[i].Cells[“img”].Value = image;

      The 6th line sets the icon “image” to the succcess_icon image from the resources. (SUT is the namespace in which I added the resource) The 7th line adds the icon “image” to the “img” cell.

      11 does the same thing.

      Reply
  1. http://google.com

    This specific article, “How to add images to
    DataGridView cell using C# and Windows Forms | John Roach” was in
    fact very good. I am making out a backup to
    demonstrate to my personal close friends. Thanks-Danuta

    Reply
    1. John Roach Post author

      Hi CoderGuy,

      There are couple ways to manage this. I have found that the best would be actually handling this not on edit but with a separate form. The reason for this is simple. When you want the user to add an image you would want to save it in a separate directory structure and maybe format it to the size of your data table. You don’t want a thumbnail on your data table not the whole thing.

      Hope this helps,

      John

      Reply

Leave a Reply