Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how to set image to DataGridViewImageColumn from image path?

Posted on 2011-03-22
17
Medium Priority
?
7,516 Views
Last Modified: 2013-12-17
hi,

I have datatable which have 3 columns id, name, imgPath
imgPath has full path of the image, such as D:/someimage.jpg

I want to set each image of its relative path to DataGridViewImageColumn that I have created in DataGridView,

so how can I do that?
0
Comment
Question by:njgroup
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
17 Comments
 
LVL 9

Expert Comment

by:jkofte
ID: 35196208
you will have to replace the text until the root directory of your website.

For let's say D:/websites/website1/images/img1.jpg and if the root directory is D:/websites/website1, you need to replace it with string.empty or "" so, you will have /images/img1.jpg

You can store D:/websites/website1 as a key in your web.config file so when you deploy your application, by changing the key with new path, you will have the same result.
0
 
LVL 9

Expert Comment

by:mayank_joshi
ID: 35196210
0
 

Author Comment

by:njgroup
ID: 35196616
hi,

this is not asp.net application, its windows application
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 6

Expert Comment

by:nandithaa
ID: 35196933
Check if this helps or not,
private void CreateColumns()
        {
            DataGridViewImageColumn imageColumn;
            Bitmap bmpImage = null;
            imageColumn = new DataGridViewImageColumn();
            bmpImage = (Bitmap)Image.FromFile(@"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Blue hills.JPG", true);
            imageColumn.Image = bmpImage;
            imageColumn.ImageLayout = DataGridViewImageCellLayout.Stretch;
            dataGridView1.Columns.Add(imageColumn);
            dataGridView1.Rows.Add();
            dataGridView1.Rows[0].Cells[0].Value = bmpImage;
            dataGridView1.Rows[0].Height = 100;

        }

Open in new window

0
 
LVL 6

Accepted Solution

by:
nandithaa earned 1000 total points
ID: 35197722
Private Sub CreateColumns()
      Dim imageColumn As DataGridViewImageColumn
      Dim bmpImage As Bitmap = Nothing
      imageColumn = New DataGridViewImageColumn()
      bmpImage = DirectCast(Image.FromFile("C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Blue hills.JPG", True), Bitmap)
      imageColumn.Image = bmpImage
      imageColumn.ImageLayout = DataGridViewImageCellLayout.Stretch
      dataGridView1.Columns.Add(imageColumn)
      dataGridView1.Rows.Add()
      dataGridView1.Rows(0).Cells(0).Value = bmpImage
      dataGridView1.Rows(0).Height = 100

End Sub
0
 

Author Comment

by:njgroup
ID: 35204762
but what if I have datatable, and it has imgPath column as image path,

I have created the DataGridViewImageColumn, but I need now foreach loop to go for each path and assign it to DataGridViewImageColumn cell, and the imgPath column should be hidden
0
 

Author Comment

by:njgroup
ID: 35204827
your code does not change the image of the cell:

                foreach (DataGridViewRow row in dgvProductsGridTMP.Rows)
                {
                    string imgPath = System.IO.Path.Combine("Z:\\", row.Cells["ImgPath"].Value.ToString());
                    Bitmap b = new Bitmap(imgPath);
                    dgvProductsGridTMP.Rows[row.Index].Cells["productImage"].Value = b;
                }
0
 
LVL 6

Expert Comment

by:nandithaa
ID: 35205107
What about now? You have to take image from the file, use (Bitmap)Image.FromFile(imgPath , true); instead of new Bitmap(imgPath);
foreach (DataGridViewRow row in dgvProductsGridTMP.Rows)
                {
                    string imgPath = System.IO.Path.Combine("Z:\\", row.Cells["ImgPath"].Value.ToString());
                    Bitmap b = (Bitmap)Image.FromFile(imgPath , true);

                    dgvProductsGridTMP.Rows[row.Index].Cells["productImage"].Value = b;
                }

Open in new window

0
 

Author Comment

by:njgroup
ID: 35205230
no, here is my code:

                foreach (DataGridViewRow row in dgvProductsGridTMP.Rows)
                {
                    string imgPath = System.IO.Path.Combine("Z:\\", row.Cells["ImgPath"].Value.ToString());
                    row.Cells["productImage"].Value = (Bitmap)System.Drawing.Image.FromFile(imgPath, true);
                }
0
 
LVL 6

Expert Comment

by:nandithaa
ID: 35212636
Your code works perfectly for me. Can u  provide me with more code if u don't mind? U've included the extension of the image rite?
Can u plz check if file exists in the directory, later u can remove that code....
foreach (DataGridViewRow row in dgvProductsGridTMP.Rows)
                {
                    string imgPath = System.IO.Path.Combine("Z:\\", row.Cells["ImgPath"].Value.ToString());
                    if (System.IO.Directory.Exists(imgPath ))
                          {
                    row.Cells["productImage"].Value = (Bitmap)System.Drawing.Image.FromFile(imgPath, true);
                           }
                           else
                           {
                                 MessageBox.Show("Invalid Path");
                           }
                }

Open in new window

0
 

Author Comment

by:njgroup
ID: 35224779
yes, I got invalid path!

but there is something strange

I had checkpoint in imgPath variable, I took the complete value pf imgPath from "Text Visualizer"  (which representing the full path "Z:\images\175.png"), and I put it in address bar of windows explorer, so, I can see the image, I dont know why it gives me incorrect path in my code!

please note that this path representing mapped driver, I have code to map a driver, and I can see mapped drive in my computer and open the image from there, and it looks like any driver (C, D, ...)

but its not working from my code!
0
 

Author Comment

by:njgroup
ID: 35225574
I got the UNC path of Z: drive (\\192.168.1.157\d$\ProductManagmentSystem\images\175.png) and that is not working for me too
0
 
LVL 6

Expert Comment

by:nandithaa
ID: 35229493
I'm sorry njgroup, i made a terrible mistake, as "mas_oz2003" pointed out in http://www.experts-exchange.com/Programming/Languages/.NET/Visual_CSharp/Q_26914351.html it is not  "Directory.Exists", instead it is "File.Exists" . I replied in hurry, so i used "Directory.Exists" without thinking.... Once again sorry for the trouble....

And as for mapped driver, plz ensure u've sufficient permission granted for accessing the image.....
0
 

Author Comment

by:njgroup
ID: 35230224
ok,
I solve the problem of network path
but the image does not displayed in data grid view, and it is displayed in normal picturebox!!!!!

                foreach (DataGridViewRow row in dgvProductsGridTMP.Rows)
                {
                    string imgPath = @"E:\Leamra Projects\ProductManagment\images\cam_product.png";
                    if (System.IO.File.Exists(imgPath))
                    {
                        pictureBox1.Image = System.Drawing.Image.FromFile(imgPath, true); \\ here it is showing the image
                        row.Cells["productImage"].Value = System.Drawing.Image.FromFile(imgPath, true); \\ here it is not showing the image
                    }
                    else
                    {
                        MessageBox.Show("Invalid Path");
                    }
                }
0
 
LVL 13

Assisted Solution

by:gamarrojgq
gamarrojgq earned 1000 total points
ID: 35291063
Hi,

Try to use Image property instead of Value like this

foreach (DataGridViewRow row in dgvProductsGridTMP.Rows)
                {
                    string imgPath = @"E:\Leamra Projects\ProductManagment\images\cam_product.png";
                    if (System.IO.File.Exists(imgPath))
                    {
                        pictureBox1.Image = System.Drawing.Image.FromFile(imgPath, true); \\ here it is showing the image
                        row.Cells["productImage"].Image = System.Drawing.Image.FromFile(imgPath, true); \\ here it is not showing the image
                    }
                    else
                    {
                        MessageBox.Show("Invalid Path");
                    }
                }
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question