Click here to Skip to main content
15,997,860 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have a DGV that displays data from a db table about different files. One of the columns displays the files type - pdf, doc etc as a string. I would like to show this with an icon instead of the text. I have added a DataGridViewImageColumn and the images I wish to display are in My Resource.

This is the code I have used to create the DGV:

With objconn
            .Close()
            If .State = ConnectionState.Closed Then
                .ConnectionString = strConnection
                .Open()
            End If
        End With

        strSQL1 = "SELECT ID, data_type, data_description AS [File Name], data_size AS [File Size (bytes)] FROM tbl_hierarchy_attachments WHERE (nodeid = " & selected_node & ")"
        Try
            da1 = New OleDbDataAdapter(strSQL1, objconn)
            Dim btn As New DataGridViewButtonColumn()
            Dim img As New DataGridViewImageColumn

            da1.Fill(ds1)
            objconn.Close()

            With dgv_ex_files

                .AllowUserToAddRows = False
                .RowHeadersVisible = False
                .RowTemplate.MinimumHeight = 50
                .DataSource = ds1.Tables(0)
                .Columns(0).Visible = False
                .Columns(1).Visible = False
                .Columns(2).Width = 450
                .ColumnHeadersDefaultCellStyle.Font = New Font("Arial", 10, FontStyle.Bold)
                .DefaultCellStyle.Font = New Font("Arial", 12, FontStyle.Regular)
                .Columns.Add(btn)
                .Columns.Add(img)
                .Columns(5).DisplayIndex = 0

            End With

            btn.HeaderText = "Open File"
            btn.Name = "file_open"
            btn.Text = "Open"
            btn.UseColumnTextForButtonValue = True
            img.HeaderText = "File Type"
            img.Name = "image"


This is the code I have tried to get the images in the image columns, but it doesn't seem to work:

VB
Private Sub dgv_ex_files_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles dgv_ex_files.CellFormatting
        If e.RowIndex < 0 Then Exit Sub

        e.FormattingApplied = True
        For Each row As DataGridViewRow In dgv_ex_files.Rows
            If row.Cells(1).Value = "pdf" Then
                row.Cells("image").Value = My.Resources.pdf
            End If
        Next

    End Sub


Could someone point me in the right direction how to get this to work?
Posted
Comments
CHill60 12-Oct-15 11:35am    
"doesn't seem to work" is not helpful - what happens?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900