Click here to Skip to main content
15,880,299 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
here is my database name called emp
i have one field pic datatye is image


i have created buttons called UPLOAD ,SAVE , DISPLAY -- on click of UPLOAD button the image is uploaded to the picturebox. on click of SAVE button the image is stored into database called emp --> image is stored in form of <binary> ...But on click of DISPLAY BUTTON the image from database is shown in DATAGRIDVIEW ...

BUT i Need help in datagridview
on click of any cell of datagridview the image from datagridview should be shown up in my PICTUREBOX


this is code for upload image in c#
try
            {
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.Filter = "png files(*.png)|*.png|jpg files(*.jpg)|*.jpg|All files(*.*)|*.*";
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    imgLoc = dlg.FileName.ToString();
                    picEmp.ImageLocation = imgLoc;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


this one is the save/insert code for image
try
          {
              byte[] img = null;
              FileStream fs = new FileStream(imgLoc, FileMode.Open, FileAccess.Read);
              BinaryReader br = new BinaryReader(fs);
              img = br.ReadBytes((int)fs.Length);
              string sql = "INSERT INTO emp values(@img)";
              if (con.State != ConnectionState.Open)
              {
                  con.Open();
                  cmd = new SqlCommand(sql,con);
                  cmd.Parameters.Add(new SqlParameter("@img",img));
                  int x = cmd.ExecuteNonQuery();
                  con.Close();
                  MessageBox.Show("SAVED SUCESSFULLY");
              }
          }
          catch (Exception ex1)
          {
              con.Close();
              MessageBox.Show(ex1.Message);
          }


this is a display code on click of display button the image is fetched from database into datagridview
string qry = "select * from emp";
          cmd = new SqlCommand(qry,con);
          da = new SqlDataAdapter(cmd);
          dt = new DataTable();
          da.Fill(dt);
          dataGridView1.DataSource = dt;
          //dataGridView1.RowTemplate.Height = 500;

          foreach (DataGridViewRow row in dataGridView1.Rows)
          {
              row.Height = 300;
          }

          DataGridViewImageColumn image = new DataGridViewImageColumn();
          image = (DataGridViewImageColumn)dataGridView1.Columns[0];
          image.ImageLayout = DataGridViewImageCellLayout.Stretch;


now i need code how to show up the image from datagridview to my picturebox onclick of particular selected row

this is the code but not working properly pls anyone provide me the running code

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {

            //picEmp.ImageLocation = dataGridView1.Rows[1].Cells[0].Value.ToString();


            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                DataGridViewRow = dataGridView1.Rows[3];
                byte[] data = row.Cells["pic"].Value as byte[];
                if (data != null)
                {
                    MemoryStream ms = new MemoryStream(data);
                    picEmp.Image = Image.FromStream(ms);
                }
            }
           
        }


What I have tried:

now i need code how to show up the image from datagridview to my picturebox onclick of particular selected row

this is the code but not working properly pls anyone provide me the running code 

<pre>private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {

            //picEmp.ImageLocation = dataGridView1.Rows[1].Cells[0].Value.ToString();


            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                DataGridViewRow = dataGridView1.Rows[3];
                byte[] data = row.Cells["pic"].Value as byte[];
                if (data != null)
                {
                    MemoryStream ms = new MemoryStream(data);
                    picEmp.Image = Image.FromStream(ms);
                }
            }
           
        }
Posted
Updated 2-Oct-17 3:47am

1 solution

When you asked this last time - this morning - I gave you the code: Need help in C# related to datagridview with picturebox[^]
I see you have used a bit of that code, but ignored the stuff that selected the row and cell ...
 
Share this answer
 
Comments
Member 12663465 2-Oct-17 10:59am    
my friend i am new to C# language learning from YouTube each and every step
so if you have the working code send me .... i like u r solution but i am another method
let me share with u the code
dataGridView1_CellContentClick


private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=imagestore; Integrated Security=True;");
SqlDataAdapter sda = new SqlDataAdapter("select * from emp",con);
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
byte[] binaryimage = (byte[])dt.Rows[0][0];
Bitmap image;
using (MemoryStream stream = new MemoryStream(binaryimage))
{
image = new Bitmap(stream);
}
pictureBox1.Image = image;
}


in this method tell how to showup the image... only one image is displayed row[0][0]
but onclick of any cell the picture in that datagridview should be displayed
i hve to traverse through loop or foreach loop for that this is the main issue i am at
OriginalGriff 2-Oct-17 11:26am    
If that is the quality of the code you are "Learning from youtube" then I'm not surprised you don't know how to do any of this yourself. There are so many things wrong with that whole idea that it difficult to know where to start! So I won't.

Stop "learning from youtube" - most of the "tutorials" there are rubbish, produced by idiots who know almost nothing about the subject, except how to get it to work in one specific case. Get yourself a book - or better a course - and follow it from beginning to end, doing all the exercises. If you don't, you know know what you missed, and it could very well be the bits that make your whole life easier.

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