1

I VB.net, i have a datagridview, that shows some results of a database query. To generate it, i executed some sql query and wrote the data to a datatable. This table is then linked to the dgview

dg_data.DataSource = SqlData.DefaultView

Now one column can contain certain (fixed and predefined) characters describing a certain status of events that are handled with my app. I would like to replace these characters with more intuitive icons.

I managed to get that working but only in a new further column (12) containing an image according to another columns (11) code character. However, i want to REPLACE the column with that char and only show the image. This is what i have:

    'Add new image column:
            Dim imgcol As New DataGridViewImageColumn()
            dg_data.Columns.Add(imgcol)
            imgcol.HeaderText = "Status"
            imgcol.Name = "img"
            imgcol.DataPropertyName = "img"
            With dg_data
                .Columns("img").DisplayIndex = 12
                .Columns("img").Width = 20
            End With

' Load images from Resources    
            Dim report_filed As Image = My.Resources.report_filed
            Dim report_due As Image = My.Resources.noreport
            Dim attention As Image = My.Resources.attention
    
' Assign correct icon for certain character in status column (example for "!")
            For i As Integer = 0 To SqlData.Rows.Count - 1
                If dg_data.Rows(i).Cells(11).Value.ToString = "!" Then
                    dg_data.Rows(i).Cells("img").Value = My.Resources.attention
                Else
                    dg_data.Rows(i).Cells("img").Value = My.Resources.report_filed
                End If
            Next i

One could of cource just discard column 11 afterwards using

  dg_data.Columns(11).Visible = False

but i am looking for a more elegant solution here, e.g.

Convert column 11 from to to img, replace "!" with image a, "c" with image "b" and so on.

Any hints for me? Thanks in advance!

Luke P
  • 73
  • 6
  • Firstly, don't add image column in code. Add it in the designer. [Here](http://www.vbforums.com/showthread.php?541476-NET) is an example of doing that with a combo box column. The principle is exactly the same with an image column, with just a few details changed. – jmcilhinney Jun 26 '20 at 06:22
  • https://stackoverflow.com/a/59238470/10216583 –  Jun 26 '20 at 06:43

0 Answers0