[英]Get a image in data gridview to picturebox in c#
我需要将数据 gridview 中的图像获取到 c# 中的图片框
这是我的 datagridview 线。
if (e.RowIndex >= 0)
{
DataGridViewRow row = this.dataGridView3.Rows[e.RowIndex];
e_id.Text = row.Cells["emp_id"].Value.ToString();
e_Fname.Text = row.Cells["emp_Fname"].Value.ToString();
e_Lname.Text = row.Cells["emp_Lname"].Value.ToString();
}
我需要在 datagridview 中单击一行,然后在图片框中加载图像。
令人惊讶的是,上面没有人提供正确的答案。 上面的请求是: “我需要在 datagridview 中单击一行,然后在图片框中加载图像。”
这是 C# 中的正确方法,或者只是使用 CType 转换为 VB 的字节数组:
MemoryStream ms = new MemoryStream((byte[])grdProducts.CurrentRow.Cells[5].Value);
picProduct.Image = Image.FromStream(ms);
MikeB443 [kd2cmo]
表单事件:
Private con As New SqlConnection("YourConnectionString")
Private com As SqlCommand
Private Sub DGV_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV.CellClick
con.Open()
com = New SqlCommand("SELECT MyPhoto FROM tbGalary WHERE ID=" & DGV.Rows(e.RowIndex).Cells(0).Value, con)
Dim ms As New MemoryStream(CType(com.ExecuteScalar, Byte()))
txtPicture.Image = Image.FromStream(ms)
txtPicture.SizeMode = PictureBoxSizeMode.StretchImage
com.Dispose()
con.Close()
End Sub
SQL表:
CREATE TABLE [dbo].[tbGalary](
[ID] [int] NOT NULL,
[MyPhoto] [image] NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
SQL 插入图像:
INSERT INTO tbGalary VALUES('1','D:\image1.jpg')
INSERT INTO tbGalary VALUES('2','D:\image2.jpg')
INSERT INTO tbGalary VALUES('3','D:\image3.jpg')
INSERT INTO tbGalary VALUES('4','D:\image4.jpg')
INSERT INTO tbGalary VALUES('5','D:\image5.jpg')
结果
用这个:
pictureBox1.Image = (Image)dataGridView3.Rows[0].Cells[1].Value;
根据您的列和行在此处使用您的索引。
如果您使用 ImageList 来存储图像,我在这种情况下所做的是将图像的索引存储在单元格的 Tag 属性中。
Image newImage = (Image)row[8].Value;
pictureBox1.Image = newImage;
要么
Image newImage = (Image)dgvDisplayTiles.CurrentRow.Cells[3].Value;
pictureBox1.Image = newImage;
row[8].Value 将根据您的 Datagridview,因为我使用的是 Superdatagrid 视图,这就是我编写 row[8].Value 的原因。
我知道已经很晚了,但我仍然很想分享我的解决方案。
关于数据网格的属性。 您将找到 CellClick。
private void sampleGrid_CellClick(object sender, DataGridViewCellEventArgs e)
{
string picpath = @"C:\Users\Public\Pictures\FolderOfYourImages\";
//"emp_Lname" or the column index that you want to use
picpath = picpath + sampleGrid["emp_Lname", grdTrans.CurrentCell.RowIndex].Value.toString() + ".jpg"
picbox.ImageLocation = picpath;
}
当您将单击数据网格时。 它将改变图片框中的图像。 对于我的示例,我选择 LastName 作为图像名称。 但这取决于您,这只是一个简单的串联。 最后,只需确保图像存在于您的路径中。
希望这仍然会有所帮助:) 快乐编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.