如何将数据从datagridview显示到picturebox? [英] How to display image from datagridview to picturebox ?

查看:109
本文介绍了如何将数据从datagridview显示到picturebox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在sql数据库中保存了图像,我在datagridview中检索了现在我想在图片框中显示这个。所有数据都在文本框中成功填充,但图像无法在图片框中获取图像。

提前致谢



我的尝试:



private void dataGridView1_RowHeaderMouseClick(object sender,DataGridViewCellMouseEventArgs e)

{

ID = Convert.ToInt32(dataGridView1.Rows [e.RowIndex] .Cells [0 ] .Value.ToString());

combo_Class.Text = dataGridView1.Rows [e.RowIndex] .Cells [1] .Value.ToString();

txt_Roll .Text = dataGridView1.Rows [e.RowIndex] .Cells [2] .Value.ToString();

txt_Name.Text = dataGridView1.Rows [e.RowIndex] .Cells [3] .Value .ToString();

txt_father.Text = dataGridView1.Rows [e.RowIndex] .Cells [4] .Value.ToString();

combo_Gender.Text = dataGridView1 .Rows [e.RowIndex] .Cells [5] .Value.ToString();

dateTimePicker1.Text = dataGridView1.Rows [e.RowIndex] .Cells [6] .Value.ToString() ;

txt_Contact.Text = dataGridView1.Rows [e.RowIndex] .Cells [7 ] .Value.ToString();

//pictureBox1.Image = dataGridView1.Rows [e.RowIndex] .Cells [8] .Value.ToString();





}

I saved image in sql database, I retrieved in datagridview now I want to show this in picture box. All data are filled successfully in textbox but image unable to get image in picture box.
Thanks in advance

What I have tried:

private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
ID = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
combo_Class.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
txt_Roll.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
txt_Name.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
txt_father.Text = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
combo_Gender.Text = dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString();
dateTimePicker1.Text = dataGridView1.Rows[e.RowIndex].Cells[6].Value.ToString();
txt_Contact.Text = dataGridView1.Rows[e.RowIndex].Cells[7].Value.ToString();
//pictureBox1.Image = dataGridView1.Rows[e.RowIndex].Cells[8].Value.ToString();


}

推荐答案

不要尝试将其转换为字符串!

从数据库获取的图像是一个字节数组 - DGV将显示为图像。

要将其用作隔离的图像,需要转换字节通过MemoryStream到一个图像:

Don't try to convert it to a string!
What you get from a DB for an Image is an array of bytes - which the DGV will display as an Image.
To use that as an Image in isolation, you need to convert the bytes to an Image via a MemoryStream:
byte[] bytes = (byte[]) myDataGridView.Rows[rowAddr].Cells[cellAddr].Value;
MemoryStream ms = new MemoryStream(bytes);
myPictureBox.Image = Image.FromStream(ms);


这篇关于如何将数据从datagridview显示到picturebox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆