【问题标题】:How do I display an image from a DataGridView to another PictureBox in a new Windows Form?如何在新的 Windows 窗体中将图像从 DataGridView 显示到另一个 PictureBox?
【发布时间】:2022-01-03 12:44:17
【问题描述】:

我正在尝试做的是,一旦我单击 DataGridView 中的一行,单击查看按钮,它会打开一个新窗口,显示从 DataGridView 到文本框的所有数据。我已经完成了第一部分,但我不知道如何处理图像(我有 2 个图像列)。有人可以帮我吗?我才刚开始,对不起。这是我打开另一个窗口的视图按钮的代码。图片框始终为空白。

Private Sub btnView_Click(sender As Object, e As EventArgs) Handles btnView.Click

    If GunaDataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected) > 0 Then
        data.txtID.Text = GunaDataGridView1.CurrentRow.Cells(0).Value.ToString
        data.txtLN.Text = GunaDataGridView1.CurrentRow.Cells(1).Value.ToString
        data.txtFN.Text = GunaDataGridView1.CurrentRow.Cells(2).Value.ToString
        data.txtMN.Text = GunaDataGridView1.CurrentRow.Cells(3).Value.ToString
        data.txtGen.Text = GunaDataGridView1.CurrentRow.Cells(4).Value.ToString
        data.txtNum.Text = GunaDataGridView1.CurrentRow.Cells(5).Value.ToString
        data.txtDOB.Text = GunaDataGridView1.CurrentRow.Cells(6).Value.ToString
        data.txtAddress.Text = GunaDataGridView1.CurrentRow.Cells(7).Value.ToString
        data.txtPlate.Text = GunaDataGridView1.CurrentRow.Cells(8).Value.ToString
        data.txtVT.Text = GunaDataGridView1.CurrentRow.Cells(9).Value.ToString
        data.txtVB.Text = GunaDataGridView1.CurrentRow.Cells(10).Value.ToString
        data.txtVYM.Text = GunaDataGridView1.CurrentRow.Cells(11).Value.ToString
        data.txtSP.Text = GunaDataGridView1.CurrentRow.Cells(12).Value.ToString
        data.ownerPhoto.Image = GunaDataGridView1.CurrentRow.Cells(13).Value
        data.carPhoto.Image = GunaDataGridView1.CurrentRow.Cells(14).Value
        data.ShowDialog()

    End If
End Sub

【问题讨论】:

  • 在我的小测试中,发布的代码“似乎”按预期工作。如果网格中的列 (13-14) 是实际的DataGridViewImageColumns,那么此代码在技术上应该可以工作。未知您如何定义网格DataSource,我们只能猜测第 13 列和第 14 列是Image 列。尝试将单元格 Value 转换为 Image 对象,例如... data.ownerPhoto.Image = CType(GunaDataGridView1.CurrentRow.Cells(13).Value, Image)
  • 此外,您的 VB 解决方案“似乎”将Option strict 设置为Off。建议您将Option Strict 转为On 用于您的VB 解决方案。如果它是“On”,那么你会在“image”行的代码中得到一个错误,抱怨它需要转换为Image。您需要记住,网格单元Value ... GunaDataGridView1.CurrentRow.Cells(13).Value ... 将返回一个Object,这可能会导致问题,但是在我的测试中,它仍然可以在选项严格设置为Off 的情况下工作。所以我只能猜测可能会发生其他事情。
  • 我不断收到此错误:无法将“System.Byte[]”类型的对象转换为“System.Drawing.Image”类型。尝试您的代码后也是同样的错误。 :(
  • 代码如何将图像添加到网格中?
  • @JohnG 我做到了,先生!我发现另一个有同样问题的人,这个评论在所有的废墟下: Dim bytes As Byte() = DataGridView1.CurrentRow.Cells(13).Value Using ms As New MemoryStream(bytes) PictureBox1.Image = Image.FromStream( ms)结束使用也感谢您的帮助,@JohnG 先生!

标签: sql vb.net datagridview crud


【解决方案1】:

我看到另一个人问了这个问题,另一个人用这个代码行评论了:

Dim bytes As Byte() = DataGridView1.CurrentRow.Cells(13).Value
    Using ms As New MemoryStream(bytes)
        ownerPP.Image = Image.FromStream(ms)
    End Using

    Dim bit As Byte() = DataGridView1.CurrentRow.Cells(14).Value
    Using memory As New MemoryStream(bit)
        carPhoto.Image = Image.FromStream(memory)
    End Using

完美运行。我希望这也能帮助别人。

【讨论】:

    猜你喜欢
    • 2019-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-26
    • 2014-08-16
    • 2023-04-03
    • 2015-03-26
    • 1970-01-01
    相关资源
    最近更新 更多