使用datagridview行单元格中的值填充图片框

时间:2017-11-22 06:36:35

标签: vb.net

我有一个datagrid(dgvMembers),其中包含所有成员的详细信息,我的挑战是将存储在datagrid的Photo列中的图片显示在CellContentClick事件之后的表单的图片框中。最初我已经成功地保存了详细信息(但我不确定图片是否与其他表单细节一起成功保存,因为我没有收到错误 - 并且列显示带有红色x的图标式图像) 。下面是btnSave保存详细信息和CellContectClick事件的代码。请帮忙,因为我现在卡住了。

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles 
btnSave.Click
Try
        OpenConnection()
        Dim ms As New MemoryStream()
        Dim bmpImage As New Bitmap(pbMyImage.Image)
        bmpImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
        Dim data As Byte() = ms.GetBuffer()
        Dim p As New OleDbParameter("@photo", OleDbType.VarBinary)
        p.Value = data
        Dim cb As String = "insert into 
 Members(TBSNum,PassNum,Firstname,Lastname,DOB,DOJ,Status,
Designation,Gender,ResAddress,Contact1,Contact2,EmailID,City,Photo)" &               
"VALUES(@tbsnum,@passnum,@fname,@lname,@dob,@doj,@status,@design,@gender,
@resaddress,@contact1,@contact2,@email,@city,@photo)"
        cmd = New OleDbCommand(cb)
        cmd.Connection = con
        cmd.Parameters.AddWithValue("@tbsnum", txtTBSNum.Text)
        cmd.Parameters.AddWithValue("@passnum", txtPassport.Text)
        cmd.Parameters.AddWithValue("@fname", txtName.Text)
        cmd.Parameters.AddWithValue("@lastname", txtSurname.Text)
        cmd.Parameters.AddWithValue("@dob", dtDOB.MaxDate)
        cmd.Parameters.AddWithValue("@doj", System.DateTime.Now.Date)
        cmd.Parameters.AddWithValue("@status", cbStatus.SelectedItem)
        cmd.Parameters.AddWithValue("@design", cbDesig.SelectedItem)
        cmd.Parameters.AddWithValue("@gender", cbGender.SelectedItem)
        cmd.Parameters.AddWithValue("@resaddress", txtAddress.Text)
        cmd.Parameters.AddWithValue("@contact1", txtPhone1.Text)
        cmd.Parameters.AddWithValue("@contact2", txtPhone2.Text)
        cmd.Parameters.AddWithValue("@email", txtEmail.Text)
        cmd.Parameters.AddWithValue("@city", txtCity.Text)
        cmd.Parameters.AddWithValue("@photo", pbMyImage.Image)
        ' cmd.Parameters.AddWithValue("@photo", data)
        cmd.ExecuteNonQuery()
        CloseConnection()
        Reset()
        Getdata()
        MessageBox.Show("Successfully saved", " TKBS Member Record", 
 MessageBoxButtons.OK, MessageBoxIcon.Information)
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, 
 MessageBoxIcon.Error)
    End Try
End Sub

CellContentClick事件的代码:

Private Sub dgvMembers_CellContentClick(sender As Object, e As 
DataGridViewCellEventArgs) Handles dgvMembers.CellContentClick
    txtTBSNum.Text = 
dgvMembers.Rows(e.RowIndex).Cells("TBSNum").Value.ToString
    txtPassport.Text = 
dgvMembers.Rows(e.RowIndex).Cells("PassNum").Value.ToString
    txtName.Text = dgvMembers.Rows(e.RowIndex).Cells("Fname").Value.ToString
    txtSurname.Text = 
dgvMembers.Rows(e.RowIndex).Cells("Surname").Value.ToString
    dtDOB.Text = dgvMembers.Rows(e.RowIndex).Cells("DOB").Value.ToString
    dtDOJ.Text = dgvMembers.Rows(e.RowIndex).Cells("DOJ").Value.ToString
    cbStatus.Text = 
dgvMembers.Rows(e.RowIndex).Cells("Status").Value.ToString
    cbDesig.Text = 
dgvMembers.Rows(e.RowIndex).Cells("Design").Value.ToString
    cbGender.Text = 
dgvMembers.Rows(e.RowIndex).Cells("Gender").Value.ToString
    txtAddress.Text = 
dgvMembers.Rows(e.RowIndex).Cells("ResAdd").Value.ToString
    txtPhone1.Text = 
dgvMembers.Rows(e.RowIndex).Cells("Phone1").Value.ToString
    txtPhone2.Text = 
dgvMembers.Rows(e.RowIndex).Cells("Phone2").Value.ToString
    txtCity.Text = dgvMembers.Rows(e.RowIndex).Cells("City").Value.ToString
    txtEmail.Text = 
dgvMembers.Rows(e.RowIndex).Cells("EmailID").Value.ToString
    txtCity.Text = dgvMembers.Rows(e.RowIndex).Cells("City").Value.ToString
    pbMyImage.Image = dgvMembers.Rows(e.RowIndex).Cells("Photo").Value
End Sub

0 个答案:

没有答案