图片框中的图像不会改变

时间:2015-01-29 14:51:31

标签: c# winforms datagridview picturebox

我在这里需要帮助。我正在使用gridview中的数据来填充文本框和pictureBox。当我点击一行时,文本框就会填满。但图片框只显示一张图片。无论我选择哪一行同一图像出现。请我是c#的新手,我需要帮助。这是我到目前为止所做的     private void viewMemberGrid_CellClick(object sender,DataGridViewCellEventArgs e)         {

        DataGridViewRow dr = viewMemberGrid.SelectedRows[0];
        GmemIDBox.Text = dr.Cells[0].Value.ToString();

        GsurnameBox.Text = dr.Cells[1].Value.ToString();
        GfirstnameBox.Text = dr.Cells[2].Value.ToString();
        GphoneBox.Text = dr.Cells[3].Value.ToString();
        memDeptBox.Text = dr.Cells[7].Value.ToString();
        memPortBox.Text = dr.Cells[8].Value.ToString();
        GaddBox.Text = dr.Cells[5].Value.ToString();
        GdobBox.Text = dr.Cells[6].Value.ToString();

        string select = "Select NEWMEMBER.MemberID, Surname, Firstname, PhoneNumber, MemberPic, HomeAddress,DOB,DeptID,PortfolioDesc from NEWMEMBER inner join Pictures on NEWMEMBER.MemberID = Pictures.MemberID join PortfolioDescription on PortfolioDescription.PortfolioId = NEWMEMBER.PortfolioID";
        SqlConnection c = new SqlConnection("Data Source=.;Initial Catalog=Cmanager;Integrated Security=True");
        SqlCommand cmd = new SqlCommand(select, c);

        try
        {
            c.Open();
            SqlDataReader dro = cmd.ExecuteReader();

            if (dro.Read())
            {
                byte[] picarr = (byte[])dro["MemberPic"];
                MemoryStream ms = new MemoryStream(picarr);
                ms.Seek(0, SeekOrigin.Begin);
                memPicBox.Image = Image.FromStream(ms);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        c.Close();

    } 

1 个答案:

答案 0 :(得分:1)

对您的选择语句进行一些更改

;

我不知道数据库中的MemberID列是否为Number或Varchar

;

如果是号码

string select = "Select NEWMEMBER.MemberID, Surname, Firstname, PhoneNumber, MemberPic, HomeAddress,DOB,DeptID,PortfolioDesc from NEWMEMBER inner join Pictures on NEWMEMBER.MemberID = Pictures.MemberID join PortfolioDescription on PortfolioDescription.PortfolioId = NEWMEMBER.PortfolioID Where NEWMEMBER.MemberID = " + dr.Cells[0].Value.ToString();

或者如果它是Varchar

string select = "Select NEWMEMBER.MemberID, Surname, Firstname, PhoneNumber, MemberPic, HomeAddress,DOB,DeptID,PortfolioDesc from NEWMEMBER inner join Pictures on NEWMEMBER.MemberID = Pictures.MemberID join PortfolioDescription on PortfolioDescription.PortfolioId = NEWMEMBER.PortfolioID Where NEWMEMBER.MemberID = '" + dr.Cells[0].Value.ToString() + "'";