我有datagridview,里面有一些列。其中一列显示了一些数字。我希望基于数字,让我们说可以有1,2或3创建额外的列,我可以根据这些数字放置图片。到目前为止我所尝试的并不像我预期的那样有效。你能告诉我我做错了吗?:
这是我的数字专栏:
dgvTransport.Columns(8).Visible = True
我的附加图片栏:
Dim img As DataGridViewImageColumn = New DataGridViewImageColumn()
img.HeaderText = "Status2"
img.Name = "Status2"
img.DisplayIndex = 16
dgvTransport.Columns.Add(img)
我将这个用于网格:
Private Sub dgvTransport_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles dgvTransport.CellFormatting
Dim sHeader As String = dgvTransport.Columns(e.ColumnIndex).Name
If sHeader = "Status2" Then
Dim LINK = dgvTransport.Rows(e.RowIndex).Cells(8).Value
If LINK.ToString.Contains("1") Then
Dim Img As New DataGridViewImageCell
Img.Value = Image.FromFile("C:\Users\Robert\Desktop\1.png")
dgvTransport.Rows(e.RowIndex).Cells(16).Value = Img.Value
End If
If LINK.ToString.Contains("2") Then
Dim Img As New DataGridViewImageCell
Img.Value = Image.FromFile("C:\Users\Robert\Desktop\2.jpg")
dgvTransport.Rows(e.RowIndex).Cells(16).Value = Img.Value
End If
If LINK.ToString.Contains("3") Then
Dim Img As New DataGridViewImageCell
Img.Value = Image.FromFile("C:\Users\Robert\Desktop\3.png")
dgvTransport.Rows(e.RowIndex).Cells(16).Value = Img.Value
End If
End If
End Sub
End If
这是在线发生的错误:
Img.Value = Image.FromFile("C:\Users\Robert\Desktop\3.png")
错误信息:
An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll
我甚至尝试用单独的方法填写图像而不是像这样的单元格格式化事件,但我在列的行中只看到空的x ..:
Private Sub PlaceImages()
For Each row As DataGridViewRow In dgvTransport.Rows
If Not row.IsNewRow Then
Dim Img As New DataGridViewImageCell
Img.Value = Image.FromFile("C:\Users\Robert\Desktop\1.png")
dgvTransport.Rows(row.Index).Cells(16) = Img
End If
Next
end Sub
我也尝试过这种方式,但也只看到红色x:
For i As Integer = 0 To dgvTransport.Rows.Count - 1
Dim sHeader As String = dgvTransport.Columns(16).Name
If sHeader = "Status2" Then
Dim LINK = dgvTransport.Rows(i).Cells(8).Value
If LINK.ToString.Contains("1") Then
Dim Img As New DataGridViewImageCell
Img.Value = My.Resources._1.GetHbitmap
dgvTransport.Rows(i).Cells(16).Value = Img
End If
If LINK.ToString.Contains("2") Then
Dim Img As New DataGridViewImageCell
Img.Value = My.Resources._2.GetHbitmap
dgvTransport.Rows(i).Cells(16).Value = Img
End If
If LINK.ToString.Contains("3") Then
Dim Img As New DataGridViewImageCell
Img.Value = My.Resources._3.GetHbitmap
dgvTransport.Rows(i).Cells(16).Value = Img
End If
End If
Next