Click here to Skip to main content
15,913,941 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
[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
C#
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;
//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;
}


private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{

//picEmp.ImageLocation = dataGridView1.Rows[1].Cells[0].Value.ToString();
????????
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)
{

//picEmp.ImageLocation = dataGridView1.Rows[1].Cells[0].Value.ToString();
????????
    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

}
Posted
Updated 1-Oct-17 22:17pm
v2

1 solution

Uue the DataGridViewCellEventArgs parameter: it gives you information on which cell was clicked.
DataGridViewCellEventArgs Class (System.Windows.Forms)[^] - specifically you want the RowIndex (and possibly ColumnIndex) property, which tells you which cell was clicked on.

From that and the DataGridView Rows property you can access the image data as a byte array, and load that into your picture box:
C#
DataGridViewRow row = myDataGridView.Rows[e.RowIndex];
byte[] data = row.Cells[e.ColumnIndex].Value as byte[];
if (data != null)
    {
    MemoryStream ms = new MemoryStream(data);
    myPictureBox.Image = Image.FromStream(ms);
    }
 
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