【问题标题】:DataGridViewImageColumn image based on text in specific columnDataGridViewImageColumn 图像基于特定列中的文本
【发布时间】:2015-02-15 06:48:41
【问题描述】:

如何根据特定列中的文本将 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"

我试图将此代码包装在“如果 DataGridView1........包含”中,但我只创建错误。谁能告诉我如何解决这个问题?

谢谢:-)

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

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。我认为我做错了什么,但看起来我走在正确的道路上。如果你愿意,请给我提示或 sn-ps,谢谢 :-)

【问题讨论】:

  • 列类型是什么无关紧要,如果要根据另一列设置一列中的值,则处理网格的CellValueChanged事件,检测源列,然后在同一行的目标列中设置值。
  • 您是从表中加载吗?
  • @jmcilhinney 太好了,现在我只需要弄清楚在 CellValueChanged 事件中如何以及使用什么代码;-)
  • @Codexer 我正在从 txt 文件加载,所以在 formload 我读取所有行并将它们放入 datagridview 中。代码 sn-p: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

标签: vb.net datagridview datagridviewimagecolumn


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-25
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    • 1970-01-01
    • 2016-03-20
    • 2021-01-16
    • 2013-04-15
    相关资源
    最近更新 更多