Click here to Skip to main content
15,879,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I need your help guys..

I want to retrieve the picture from datagridview to picturebox. Below is my code and I am getting error in datagridview cell click event :( Can you guys help me? I badly need your help :(

What I have tried:

Here`s my code for browsing picture into picturebox:

C#
string img_file = null;
        private void button13_Click(object sender, EventArgs e)
        {
            OpenFileDialog o = new OpenFileDialog();
            if (o.ShowDialog() != DialogResult.Cancel)
            {
                img_file = o.FileName;
                pictureBox1.Image = Image.FromFile(o.FileName);
            }
        }

While here`s my code for saving image to database:


if (img_file != null)
            {

                FileStream fs = new FileStream(img_file, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                byte[] image = new byte[fs.Length];
                fs.Read(image, 0, Convert.ToInt32(fs.Length));
                fs.Close();

                if (textBox2.Text != "" && comboBox13.Text != "" && comboBox1.Text != "" && dateTimePicker1.Text != "" && comboBox3.Text != "" && textBox4.Text != "" && textBox5.Text != "" && textBox6.Text != "" && textBox7.Text != "")
                {
                    SqlConnection con = new SqlConnection(cs);
                    SqlCommand cmd = new SqlCommand("insert into tbl_Inquiry(Dog_Name,Breed,Color,Birthday,Gender,Size,Owners_Last_Name,Owners_First_Name,Contact_No,Address,Date_Registered,Image) values (@dogname,@breed,@color,@birthday,@gender,@size,@lastname,@firstname,@contactno,@address,@date,@image)", con);
                    con.Open();
                    cmd.Parameters.AddWithValue("@dogname", textBox2.Text);
                    cmd.Parameters.AddWithValue("@breed", comboBox13.Text);
                    cmd.Parameters.AddWithValue("@color", comboBox1.Text);
                    cmd.Parameters.AddWithValue("@birthday", dateTimePicker1.Text);
                    cmd.Parameters.AddWithValue("@gender", comboBox2.Text);
                    cmd.Parameters.AddWithValue("@size", comboBox3.Text);
                    cmd.Parameters.AddWithValue("@lastname", textBox5.Text);
                    cmd.Parameters.AddWithValue("@firstname", textBox6.Text);
                    cmd.Parameters.AddWithValue("@contactno", textBox7.Text);
                    cmd.Parameters.AddWithValue("@address", textBox4.Text);
                    cmd.Parameters.AddWithValue("@date", DateTime.Now.ToShortDateString());
                    SqlParameter prm = new SqlParameter("@image", SqlDbType.Image, image.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, image);
                    cmd.Parameters.Add(prm);
                    cmd.ExecuteNonQuery();
                    con.Close();

                   
                    MessageBox.Show("Sucessfully Save!");
                    DisplayData();
                    ClearData();
                }
                else
                {
                    MessageBox.Show("Missing details! Please try again!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }

And lastly, here`s my code for retrieving image from datagridview to picturebox:


    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            textBox1.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            textBox2.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
            comboBox13.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
            comboBox1.Text = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
            dateTimePicker1.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
            comboBox2.Text = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
            comboBox3.Text = dataGridView1.SelectedRows[0].Cells[6].Value.ToString();
            textBox5.Text = dataGridView1.SelectedRows[0].Cells[7].Value.ToString();
            textBox6.Text = dataGridView1.SelectedRows[0].Cells[8].Value.ToString();
            textBox7.Text = dataGridView1.SelectedRows[0].Cells[9].Value.ToString();
            textBox4.Text = dataGridView1.SelectedRows[0].Cells[10].Value.ToString();

byte[] bytes = (byte[]) dtaGridView1.SelectedRows[0].Cells[11].Value;
MemoryStream ms = new MemoryStream(bytes);
pictureBox1.Image = Image.FromStream(ms);
}
Posted
Updated 29-Sep-18 19:28pm
v2

1 solution

C#
byte[] bytes = (byte[]) dtaGridView1.SelectedRows[0].Cells[11].Value;
MemoryStream ms = new MemoryStream(bytes);
System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
pictureBox1.Image = img;
 
Share this answer
 

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