EliasMP EliasMP - 3 months ago 30
C# Question

Cant add image to DataGridView Dinamically

Using C# and Webforms, the code below:

DataGridViewImageColumn img = new DataGridViewImageColumn();
string path = "path" + file;
Image image = Image.FromFile(path);
img.Image = image;
DataGridView.Columns.Add(img);
img.HeaderText = "Picture";
img.Name = "picture";


My problem is the next one. This code is not working... DataGridview is not showing the image.
When I debugged this code, the path is correct, and I load the Image properly in img element. Even more, when I´m using several images, the DataGridView is showing the same number of columns that I´m adding. But not any image at all is displayed, I only can see the typical white square within red cross...

What is the problem?

As usual, thank you guys...

TaW TaW
Answer

You can add the image(s) to the Cells like this:

dataGridView1[yourColumn, yourRow].Value = Image.FromFile(path); 

Of course you can use any other image source.. This way you can load different images to each Row..

Note: If you really only want to add the same image to each Row you can do that with your code, but you need to add real rows.

It will not show in the last row, when AllowUserToAddRows is on but will show on all other rows you add..

Comments