当我在c#中单击我的datagridview行时,如何让我的图片显示到picturebox1

时间:2013-09-20 18:31:04

标签: c# datagridview selectionchanged

我有一个这个程序,如果用户点击datagridview中的一行:

    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();
                TxtFilePath.Text = ListView.Rows[e.RowIndex].Cells[8].Value.ToString();

                BtnAdd.Enabled = false;

            }
            else
            {
                ClearData();
            }
        }
        catch
        {

        }
    }

并且读取数据库以便它可以获取路径并在用户单击datagridview中的行时显示我保存在数据库中的图像到picturebox1。

    public void TabindexChanged()
    {
        MemoryStream ms = new MemoryStream();

        cmd = new SqlCommand("SELECT * FROM DBAdmin WHERE imgPath='" + _dg + "'", sc);
        sc.Open();
        SqlDataReader dr;

        try
        {
            dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                byte[] picarr = (byte[])dr["imgImage"];
                ms = new MemoryStream(picarr);
                ms.Seek(0, SeekOrigin.Begin);
                _pb.Image = Image.FromStream(ms);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            sc.Close();
        }
    }

但它似乎不会从数据库中获取imgPath .. 每当选择一行时,不会在picturebox1中显示任何图片。 提前谢谢。

0 个答案:

没有答案