【发布时间】: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