Basically, since the images are NOT coming from a DB but are embedded in the project as shown from the solution explorer picture, your code is going to have to “build” this DataTable
using those embedded resources.
Is what it appears you are trying to do is “add” the pictures to the grids “column.” You want to add the images to the DataTable
ROWS. Then, it will be unnecessary to “add” the “grid image” column to the grid. The grid will know how to display the images from the DataTable
since they are “actual” images.
Below is a small example with only one image column, however it should not be difficult to add the other columns using the same strategy. Also, I am getting the images that are embedded as a resource in the project as your current picture shows. You will need to change the text that says “ProjectName” with the name of your project in addition to the image file names.
DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("JpgPath", typeof(Image));
Assembly myAssembly = Assembly.GetExecutingAssembly();
Stream myStream = myAssembly.GetManifestResourceStream("ProjectName.Resources.Picture.jpg.image1.jpg");
Image jpg1 = new Bitmap(myStream);
myStream = myAssembly.GetManifestResourceStream("ProjectName.Resources.Picture.jpg.image2.jpg");
Image jpg2 = new Bitmap(myStream);
myStream = myAssembly.GetManifestResourceStream("ProjectName.Resources.Picture.jpg.image3.jpg");
Image jpg3 = new Bitmap(myStream);
dt.Rows.Add("Name1", jpg1);
dt.Rows.Add("Name2", jpg2);
dt.Rows.Add("Name3", jpg3);
dgv.DataSource = dt;
Edit from OP question.
The example below shows two methods. The first is a simulation of getting the data from the DB where the images are not there but the string paths to the images are.
Once we have the DataTable
from the DB, we need to “add” those images to each row in the existing DataTable
. Obviously, we need to “add” the new “Image” column to the existing DataTable
we got from the DB. Then loop through each row and add the image to the image column based on the path in that row.
private void Form1_Load(object sender, EventArgs e) {
DataTable DBTable = GetTableFromDB();
AddImageColumnToDT(DBTable);
dgv.DataSource = DBTable;
}
private DataTable GetTableFromDB() {
DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("ImagePath", typeof(string));
dt.Rows.Add("Name1", "PathTo_Image1");
dt.Rows.Add("Name2", "PathTo_Image2");
dt.Rows.Add("Name3", "PathTo_Image3");
return dt;
}
private void AddImageColumnToDT(DataTable dt) {
dt.Columns.Add("Image", typeof(Image));
string curPath;
Image curImage;
foreach (DataRow row in dt.Rows) {
curPath = row["ImagePath"].ToString();
curImage = Image.FromFile(curPath);
row["Image"] = curImage;
}
}
EDIT*** if the DB returns a "empty" byte[]
array column instead of an Image
type.
private DataTable GetTableFromDB2() {
DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("ImagePath", typeof(string));
dt.Columns.Add("Image", typeof(byte[]));
dt.Rows.Add("Name1", "PathTo_Image1");
dt.Rows.Add("Name2", "PathTo_Image2");
dt.Rows.Add("Name3", "PathTo_Image3");
return dt;
}
private void AddImageColumnToDT2(DataTable dt) {
//dt.Columns.Add("Image", typeof(byte[]));
string curPath;
Image curImage;
byte[] curByteArray;
foreach (DataRow row in dt.Rows) {
curPath = row["ImagePath"].ToString();
curImage = Image.FromFile(curPath);
curByteArray = imageToByteArray(curImage);
row["Image"] = curByteArray;
}
}
public byte[] imageToByteArray(Image imageIn) {
using (MemoryStream ms = new MemoryStream()) {
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
}