简体   繁体   中英

DatagridView picture image to PictureBox1 in C#

Can you please help me solve my problem. I have a Datagridview and a picturebox.. I already have my database connected to my datagridview when form loads.. When I select a row in it I should get the Image file from database to the PictureBox1. How do i do that? I still can't post pictures .. Please can anyone help me? Thank you..

    private void ListView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        try
        {
            if (!ListView.Rows[e.RowIndex].IsNewRow)
            {
                idnum.Text = ListView.Rows[e.RowIndex].Cells[0].Value.ToString();
                CmbPosition.Text = ListView.Rows[e.RowIndex].Cells[1].Value.ToString();
                TxtFirstName.Text = ListView.Rows[e.RowIndex].Cells[2].Value.ToString();
                TxtMiddleName.Text = ListView.Rows[e.RowIndex].Cells[3].Value.ToString();
                TxtLastName.Text = ListView.Rows[e.RowIndex].Cells[4].Value.ToString();
                CmbYearLevel.Text = ListView.Rows[e.RowIndex].Cells[5].Value.ToString();
                CmbCourse.Text = ListView.Rows[e.RowIndex].Cells[6].Value.ToString();
                TxtSchoolYear.Text = ListView.Rows[e.RowIndex].Cells[7].Value.ToString();

                //Classes.MainFormClass ret = new Classes.MainFormClass();
                //ret.retPic();

                BtnAdd.Enabled = false;
            }
            else
            {
                ClearData();
            }
        }
        catch
        {

        }

for now, i have that code, when i click a row in the datagridview, it gets information from the database and shows in the controls = idnum.Text, CmbPosition.Text, etc... But the image does not show in Picturebox1.

I know the answer now.

    PicBox1.Image = Image.FromFile(ListView.Rows[e.RowIndex].Cells[8].Value.ToString(), true);

Just have to put that code inside my if statement. Hope this helps to anyone who has same problem as me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM