Reputation: 13
I need to get an image in data gridview to picturebox in c#
This is my datagridview cord.
if (e.RowIndex >= 0)
{
DataGridViewRow row = this.dataGridView3.Rows[e.RowIndex];
e_id.Text = row.Cells["emp_id"].Value.ToString();
e_Fname.Text = row.Cells["emp_Fname"].Value.ToString();
e_Lname.Text = row.Cells["emp_Lname"].Value.ToString();
}
I need to click a row in datagridview then load image in picturebox.
Upvotes: 1
Views: 21877
Reputation: 448
Image newImage = (Image)row[8].Value;
pictureBox1.Image = newImage;
or
Image newImage = (Image)dgvDisplayTiles.CurrentRow.Cells[3].Value;
pictureBox1.Image = newImage;
row[8].Value will be according to your Datagridview because I am using Superdatagrid view that's why I wrote row[8].Value.
Upvotes: 0
Reputation: 51
Surprisingly but NOBODY above provided the correct answer. The request above was: "I need to click a row in datagridview then load image in picturebox."
Here is the correct way in C# or just use CType to cast to byte array for VB:
MemoryStream ms = new MemoryStream((byte[])grdProducts.CurrentRow.Cells[5].Value);
picProduct.Image = Image.FromStream(ms);
MikeB443 [kd2cmo]
Upvotes: 5
Reputation: 473
I know that it already late but I still really want to share my solution.
on the properties of datagrid. You will found CellClick.
private void sampleGrid_CellClick(object sender, DataGridViewCellEventArgs e)
{
string picpath = @"C:\Users\Public\Pictures\FolderOfYourImages\";
//"emp_Lname" or the column index that you want to use
picpath = picpath + sampleGrid["emp_Lname", grdTrans.CurrentCell.RowIndex].Value.toString() + ".jpg"
picbox.ImageLocation = picpath;
}
When you will click the datagrid. It will change the image in picture box. For my sample, I choose the LastName to be the image name. But It will be up to you it will be just a simple concatenation. And Lastly, Just make sure that the image is existing in your path.
Hope this will still help :) Happy Coding.
Upvotes: -1
Reputation: 15
Form Event:
Private con As New SqlConnection("YourConnectionString")
Private com As SqlCommand
Private Sub DGV_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV.CellClick
con.Open()
com = New SqlCommand("SELECT MyPhoto FROM tbGalary WHERE ID=" & DGV.Rows(e.RowIndex).Cells(0).Value, con)
Dim ms As New MemoryStream(CType(com.ExecuteScalar, Byte()))
txtPicture.Image = Image.FromStream(ms)
txtPicture.SizeMode = PictureBoxSizeMode.StretchImage
com.Dispose()
con.Close()
End Sub
SQL Table:
CREATE TABLE [dbo].[tbGalary](
[ID] [int] NOT NULL,
[MyPhoto] [image] NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
SQL Insert image:
INSERT INTO tbGalary VALUES('1','D:\image1.jpg')
INSERT INTO tbGalary VALUES('2','D:\image2.jpg')
INSERT INTO tbGalary VALUES('3','D:\image3.jpg')
INSERT INTO tbGalary VALUES('4','D:\image4.jpg')
INSERT INTO tbGalary VALUES('5','D:\image5.jpg')
Result
Video link: Retrieve an image in DataGridView load to PictureBox in VB.NET
Upvotes: 1
Reputation: 3122
If you are using an ImageList to store the images, what I do in this case is store the index of the image in the Tag property of the cell.
Upvotes: 0
Reputation: 12439
Use this:
pictureBox1.Image = (Image)dataGridView3.Rows[0].Cells[1].Value;
Use your indexes here according to your column and the row.
Upvotes: 0