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;
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)
{
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)
{
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);
}
}
}