Get a image in data gridview to picturebox in c#

混江龙づ霸主 提交于 2019-12-11 06:45:54

问题


I need to get an image in data gridview to picturebox in c#

This is my datagridview cord.

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();

}

I need to click a row in datagridview then load image in picturebox.


回答1:


Surprisingly but NOBODY above provided the correct answer. The request above was: "I need to click a row in datagridview then load image in picturebox."

Here is the correct way in C# or just use CType to cast to byte array for VB:

 MemoryStream ms = new MemoryStream((byte[])grdProducts.CurrentRow.Cells[5].Value);
 picProduct.Image = Image.FromStream(ms);

MikeB443 [kd2cmo]




回答2:


Here is my solution. It works correctly for me to retrieve an image from DataGridView to load in PictureBox.

Form Event:

 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 Table:

CREATE TABLE [dbo].[tbGalary](
    [ID] [int] NOT NULL,
    [MyPhoto] [image] NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

SQL Insert image:

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')

Result

Video link: Retrieve an image in DataGridView load to PictureBox in VB.NET




回答3:


Use this:

pictureBox1.Image = (Image)dataGridView3.Rows[0].Cells[1].Value;

Use your indexes here according to your column and the row.




回答4:


If you are using an ImageList to store the images, what I do in this case is store the index of the image in the Tag property of the cell.




回答5:


I know that it already late but I still really want to share my solution.

on the properties of datagrid. You will found 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;
            }

When you will click the datagrid. It will change the image in picture box. For my sample, I choose the LastName to be the image name. But It will be up to you it will be just a simple concatenation. And Lastly, Just make sure that the image is existing in your path.

Hope this will still help :) Happy Coding.



来源:https://stackoverflow.com/questions/17334843/get-a-image-in-data-gridview-to-picturebox-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!