首页 > 解决方案 > 将图像动态加载到 datagridview 并且无法将图像正确加载到单个单元格中

问题描述

我正在尝试编写一个程序,我可以将图像从资源管理器拖到树视图中并以基本方式/分组进行排序,以便我可以导出到报表生成器的 Word 文档中。目前我必须手动完成所有工作,添加表格和详细信息,如文件名、日期和时间。

到目前为止,我已经构建了这个,目前正坚持要显示正确的图像。我认为在添加第二行(以及更多行)时需要做一些事情,因为我无法更改单元格的值。

我在下面粘贴了程序的一部分以及我希望实现的一些屏幕截图。

我认为它与datagridviewimagecell有关,但实现它的运气并不好。我已经恢复的这段代码现在可以工作了。如果您需要其余代码,请告诉我。我在网上尝试了很多示例,但似乎无法正常工作。

基本上我正在调用 SearchTheTreeView,搜索每个父节点,然后添加行,调用 RecursiveSearch 并将每个节点添加到行中。循环遍历每个父节点并根据树视图列表添加子节点。

   Private Function SearchTheTreeView(ByVal TV As TreeView, ByVal TextToFind As String, ByVal AddingFiles As Boolean) As TreeNode
        '  Empty previous
        NodesThatMatch.Clear()

        Dim TVcount = TV.Nodes.Count
        Dim TVnode = TV.Nodes
        'Dim imageColumn As DataGridViewImageColumn

        DataGridView1.Rows().Clear()
        DataGridView1.Columns.Clear()

        'imageColumn = New DataGridViewImageColumn()
        DataGridView1.Columns.Add("Group Ref", "")

        Dim i As Integer

        i = 0

        For Each TN As TreeNode In TV.Nodes
            If AddingFiles = True Then

                DataGridView1.Rows.Add()

                If TN.Name IsNot "Group" Then
                    Dim fimg As New DataGridViewImageColumn()
                    Dim finImg As Image = OrientedImageFromFile(TN.Text)
                    fimg.Image = finImg


                    DataGridView1.Rows(i).Cells(1).Value = fimg

                    For c = 2 To DataGridView1.ColumnCount
                        'loop through here to later to change red cross to blank image

                    Next
                    'setting to nothing as a test to see if anything changed / worked with showing images on rows below first
                    'fimg = Nothing
                    'finImg = Nothing
                End If

            End If
            RecursiveSearch(TN, TextToFind, AddingFiles, i)

            i += 1

        Next

    End Function

Private Sub RecursiveSearch(ByVal treeNode As TreeNode, ByVal TextToFind As String, AddingFilesRecursive As Boolean, ByVal rowID As Integer)

        'run through each of the child nodes 
        'get node count of referred parent
        Dim TNcount = treeNode.Nodes.Count

        'Reference counter for each node
        Dim ii As Integer
        ii = 0

        For Each TN As TreeNode In treeNode.Nodes
            'check that we are adding images to the datagrid
            If AddingFilesRecursive = True Then
                'if image is not a group
                If TN.Text IsNot "Group" Then
                    'add image as new column for first row
                    Dim img As New DataGridViewImageColumn()
                    Dim inImg As Image = OrientedImageFromFile(TN.Text)
                    img.Image = inImg
                    img.ValuesAreIcons = False

                    ' First images added will add new columns, [else] need to add to cells
                    If ii + 1 >= DataGridView1.ColumnCount Then
                        'add images in columns --------------------------this works first row ok!
                        DataGridView1.Columns.Add(img)

                    Else

                        'add images to cells ----------------------------- this is where is comes unstuck
                        'same images are added for each row from previous subroutine
                        'then this image added, but red cross shown
                        DataGridView1.Rows(rowID).Cells(ii + 1).Value = img


                    End If
                End If
            End If

            NodesThatMatch.Add(TN)

            'Follwing line not needed as will never get this far due to restrictions on node level
            'RecursiveSearch(TN, TextToFind, AddingFilesRecursive, rowID)
            ii += 1
        Next



    End Sub

Private Function OrientedImageFromFile(ByVal photoFileName As String) As Image

    On Error GoTo ErrorHandler

    'Get the image  
    Dim img As Image = Image.FromFile(photoFileName)

    'read the file and find the orientation of the image
    Dim pi As Imaging.PropertyItem = Array.Find(img.PropertyItems,
              Function(T As Imaging.PropertyItem) T.Id = &H112) '&H112 = PropertyTagOrientation
    'set the correct orientaiton
    Select Case pi.Value(0) 'value is array of Int16
        Case 3 : img.RotateFlip(RotateFlipType.Rotate180FlipNone) 'upside-down
        Case 6 : img.RotateFlip(RotateFlipType.Rotate90FlipNone) 'rotated right
        Case 8 : img.RotateFlip(RotateFlipType.Rotate270FlipNone) 'rotated left
    End Select

    'get the final image height from the textbox1
    Dim TargetHeight As Integer
    Dim TargetWidth As Integer

    TargetHeight = TextBox1.Text
    TargetWidth = TextBox1.Text

    'Set the final image height and width propotionally
    Dim NewHeight As Integer = TargetHeight
    Dim NewWidth As Integer = NewHeight / img.Height * img.Width

    If NewWidth > TargetWidth Then
        NewWidth = TargetWidth
        NewHeight = NewWidth / img.Width * img.Height
    End If

    'Return the image with new height and width and orientated correctly
    Return New Bitmap(img, NewWidth, NewHeight)

ErrorHandler:
    MsgBox("Error!" & vbCrLf & Err.Description)


End Function

这是它外观的屏幕截图,并在其上标记了它应该是什么。图像去哪里的绿色数字,然后为没有组或组中图像较少的图像清空其他单元格。我还没有做的事情,但是当我可以正确加载图像时会做。

似乎它为每一行添加了相同的图像,但不确定这是如何发生的。

正在发生的事情以及我希望实现的目标

标签: vb.netdatagridviewdatagridviewimagecolumn

解决方案


推荐阅读