在C#中将DatagridView图片图片添加到PictureBox1

时间:2021-09-29 15:51:21

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..

你能帮我解决一下我的问题吗?我有一个Datagridview和一个图片框..当表单加载时,我已经将我的数据库连接到我的datagridview ..当我在其中选择一行时,我应该从数据库中获取Image文件到PictureBox1。我怎么做?我仍然无法发布图片..请任何人可以帮助我吗?谢谢..

    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.

现在,我有那个代码,当我点击datagridview中的一行时,它从数据库中获取信息并显示在controls = idnum.Text,CmbPosition.Text等...但图片不显示在Picturebox1中。

1 个解决方案

#1


0  

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.

只需将该代码放在我的if语句中。希望这有助于任何与我有同样问题的人。

#1


0  

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.

只需将该代码放在我的if语句中。希望这有助于任何与我有同样问题的人。