njgroup
asked on
how to set image to DataGridViewImageColumn from image path?
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?
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?
ASKER
hi,
this is not asp.net application, its windows application
this is not asp.net application, its windows application
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;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
ASKER
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[ro w.Index].C ells["prod uctImage"] .Value = b;
}
foreach (DataGridViewRow row in dgvProductsGridTMP.Rows)
{
string imgPath = System.IO.Path.Combine("Z:
Bitmap b = new Bitmap(imgPath);
dgvProductsGridTMP.Rows[ro
}
What about now? You have to take image from the file, use (Bitmap)Image.FromFile(img Path , 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;
}
ASKER
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.Ima ge.FromFil e(imgPath, true);
}
foreach (DataGridViewRow row in dgvProductsGridTMP.Rows)
{
string imgPath = System.IO.Path.Combine("Z:
row.Cells["productImage"].
}
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....
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");
}
}
ASKER
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!
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!
ASKER
I got the UNC path of Z: drive (\\192.168.1.157\d$\Produc tManagment System\ima ges\175.pn g) and that is not working for me too
I'm sorry njgroup, i made a terrible mistake, as "mas_oz2003" pointed out in https://www.experts-exchange.com/questions/26914351/why-network-path-or-mapped-driver-path-is-not-working.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.....
And as for mapped driver, plz ensure u've sufficient permission granted for accessing the image.....
ASKER
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.p ng";
if (System.IO.File.Exists(img Path))
{
pictureBox1.Image = System.Drawing.Image.FromF ile(imgPat h, true); \\ here it is showing the image
row.Cells["productImage"]. Value = System.Drawing.Image.FromF ile(imgPat h, true); \\ here it is not showing the image
}
else
{
MessageBox.Show("Invalid Path");
}
}
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\
if (System.IO.File.Exists(img
{
pictureBox1.Image = System.Drawing.Image.FromF
row.Cells["productImage"].
}
else
{
MessageBox.Show("Invalid Path");
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
For let's say D:/websites/website1/image
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.