Click here to Skip to main content
15,846,212 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I saved image in sql database, I retrieved in datagridview now I want to show this in picture box. All data are filled successfully in textbox but image unable to get image in picture box.
Thanks in advance

What I have tried:

private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
ID = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
combo_Class.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
txt_Roll.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
txt_Name.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
txt_father.Text = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
combo_Gender.Text = dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString();
dateTimePicker1.Text = dataGridView1.Rows[e.RowIndex].Cells[6].Value.ToString();
txt_Contact.Text = dataGridView1.Rows[e.RowIndex].Cells[7].Value.ToString();
//pictureBox1.Image = dataGridView1.Rows[e.RowIndex].Cells[8].Value.ToString();

Updated 24-May-17 9:08am

1 solution

Don't try to convert it to a string!
What you get from a DB for an Image is an array of bytes - which the DGV will display as an Image.
To use that as an Image in isolation, you need to convert the bytes to an Image via a MemoryStream:
byte[] bytes = (byte[]) myDataGridView.Rows[rowAddr].Cells[cellAddr].Value;
MemoryStream ms = new MemoryStream(bytes);
myPictureBox.Image = Image.FromStream(ms);
Share this answer
Member 13220754 24-May-17 14:30pm    
I user this code, but error in Image ("Image.FromStream(ms)
Can you help me?
OriginalGriff 24-May-17 14:36pm    
Let me guess ... the error is a "Parameter is not valid" exception?

What that means is that your code is very likely wide open to SQL Injection which will let any user damage or destroy your database when he feels like it. Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

Now, to fix the problem, see here:

And read it all, not just skim the highlights and copy the code...
Member 13220754 24-May-17 14:41pm    
I have this code for those colluns
txtIDMovie.Text = dgvMovies.Rows[e.RowIndex].Cells[0].Value.ToString();
txtNameMovie.Text = dgvMovies.Rows[e.RowIndex].Cells[1].Value.ToString();
txtRealizador.Text = dgvMovies.Rows[e.RowIndex].Cells[2].Value.ToString();
txtCatMovies.Text = dgvMovies.Rows[e.RowIndex].Cells[3].Value.ToString();
txtAnoMovie.Text = dgvMovies.Rows[e.RowIndex].Cells[4].Value.ToString();
txtClassIdade.Text = dgvMovies.Rows[e.RowIndex].Cells[5].Value.ToString();
txtClassIMDB.Text = dgvMovies.Rows[e.RowIndex].Cells[6].Value.ToString();
cbFormato.Text = dgvMovies.Rows[e.RowIndex].Cells[7].Value.ToString();

And i tested your code for show image in picture box
OriginalGriff 25-May-17 3:09am    
Follow the link I gave you, and read it. Your code doesn't even try to display an image ... or show any database manipulation ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900