DataGridViewImageColumn image based on text in specific column

2.2k views Asked by At

how can i add specific images in a ImageColumn into a DataGridView based on text in a specific column?

example:

Column1 Column2 Column3 ImageColumn
website title info logo

I want the logo/image to be changed and show the "right" logo for each website and not the same image for all websites.

i now have this to add nice logos but it just add the same logo on every single row.

Dim img As New DataGridViewImageColumn()
        Dim inImg As Image = PictureBox1.Image
        img.Image = inImg
        DataGridView1.Columns.Add(img)
        img.HeaderText = "Website"
        img.Name = "img"

i have tried to wrap this code in a "if DataGridView1........contains" but i only creating errors. Can someone tell me a bit how to tackle this problem?

Thanks :-)

UPDATE: i now use this code:

Private Sub DataGridView1_CellFormatting(sender As Object, e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting

    If DataGridView1.Rows.Count > 0 Then

        If e.ColumnIndex = 2 Then
            Dim LINK = DataGridView1.Rows(e.RowIndex).Cells(0).Value
            If LINK.ToString.Contains("test.nl") Then

                DataGridView1.Rows(e.RowIndex).Cells(5).Value = PictureBox1.Image
            End If

        End If

    End If

End Sub

Seems to be working but when i use this code nothing is changed with the images:

Private Sub DataGridView1_CellFormatting(sender As Object, e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting

    If DataGridView1.Rows.Count > 0 Then

        If e.ColumnIndex = 2 Then
            Dim LINK = DataGridView1.Rows(e.RowIndex).Cells(0).Value
            If LINK.ToString.Contains("test.nl") Then

                DataGridView1.Rows(e.RowIndex).Cells(5).Value = PictureBox1.Image
            End If
            If LINK.ToString.Contains("test.com") Then

                DataGridView1.Rows(e.RowIndex).Cells(5).Value = PictureBox2.Image
            End If
        End If

    End If

End Sub

all images are the same... they all use pictureboximage2. I assume i do something wrong, but it looks like i am on the right way. Hit me with tips or snippets if you want to, thanks :-)

1

There are 1 answers

0
Maarten On BEST ANSWER

in

DataGridView1.CellFormatting

use this:

If DataGridView1.Rows.Count > 0 Then


        Dim LINK = DataGridView1.Rows(e.RowIndex).Cells(0).Value
        If LINK.ToString.Contains("test.nl") Then

            DataGridView1.Rows(e.RowIndex).Cells(5).Value = PictureBox1.Image
        End If

        If LINK.ToString.Contains("test.com") Then

            DataGridView1.Rows(e.RowIndex).Cells(5).Value = PictureBox2.Image
        End If

    End If

Please hit me when you see a problem in my code. Thank you all :-)