[this is the GUI of C# i made][1]
[1]: https://i.stack.imgur.com/z6nut.png
i need help the data is showing in the datagridview
but how to fetch the data of selected row from datagridview to my picturebox
when i click any row of datagridview the picture from datagridview show be seen
in the my picturebox but i dont know how to get it tried a lot ... pls need help
i used the sqlexpress created tabel emp and filed like pic and datatype as image
this is the code...of mine
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.Drawing.Imaging;
namespace image_upload_retrieve
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=imagestore; Integrated Security=True;");
SqlCommand cmd;
SqlDataAdapter da;
DataTable dt;
string imgLoc = "";
private void image_Click(object sender, EventArgs e)
{
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);
}
}
private void imagesaving_Click(object sender, EventArgs e)
{
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);
}
}
private void displayimage_Click(object sender, EventArgs e)
{
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;
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
????????
What kind of code should I write here to on click of datagridview
Cell the image should be automatically shown on the picturebox
}
private void button1_Click(object sender, EventArgs e)
{
if (picEmp.Image != null)
{
Image img = picEmp.Image;
picEmp.Image = null;
img.Dispose();
}
}
}
}
What I have tried:
problem with this code
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
????????
i above code i have separaete button display on click of it the database pictures are loaded to my datagridview now from datagrid cell click event i want to show up image on my picturebox
What kind of code should I write here to on click of datagridview
Cell the image should be automatically shown on the picturebox
}