DataGridViewImageColumn图像基于特定列中的文本

时间:2014-12-16 20:34:05

标签: vb.net datagridview datagridviewimagecolumn

如何根据特定列中的文本将ImageColumn中的特定图像添加到DataGridView中?

示例:

Column1 Column2 Column3 ImageColumn
网站标题信息徽标

我希望更改徽标/图片,并为每个网站显示“正确”的徽标,而不是所有网站的相同图片。

我现在要添加漂亮的徽标,但它只是在每一行添加相同的徽标。

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

我试图将此代码包装在“if DataGridView1 ........ contains”中,但我只创建错误。有人能告诉我一些如何解决这个问题吗?

谢谢: - )

更新: 我现在使用这段代码:

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

似乎工作正常,但是当我使用此代码时,图像没有任何改变:

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

所有图像都相同......它们都使用pictureboximage2。我认为我做错了什么,但看起来我的方式正确。如果你愿意,可以用提示或片段打我,谢谢: - )

1 个答案:

答案 0 :(得分:0)

in

  

DataGridView1.CellFormatting

使用它:

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

当您在我的代码中发现问题时,请打我。谢谢大家: - )