0

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 :-)

Maarten
  • 57
  • 1
  • 3
  • 14
  • It doesn't matter what the column types are, if you want to set the value in one column based on another column then you handle the `CellValueChanged` event of the grid, detect a change in the source column and then set the value in the destination column of the same row. – jmcilhinney Dec 17 '14 at 00:08
  • Are you loading from a table? – Trevor Dec 17 '14 at 01:27
  • @jmcilhinney great, now i just need to figure out how and what code to use in the CellValueChanged event ;-) – Maarten Dec 17 '14 at 13:00
  • @Codexer i am loading from a txt file, so on formload i read all lines and put them in the datagridview. code snippet: `Dim fName As String = "csv.txt" Dim dtTest As New DataTable("dtTest") Dim TextLine As String = "" Dim SplitLine() As String If System.IO.File.Exists(fName) = True Then Dim objReader As New System.IO.StreamReader(fName) Do While objReader.Peek() <> -1 TextLine = objReader.ReadLine() SplitLine = Split(TextLine, "|") Me.DataGridView1.Rows.Add(SplitLine) Loop` – Maarten Dec 17 '14 at 13:03

1 Answers1

0

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 :-)

Maarten
  • 57
  • 1
  • 3
  • 14