首页 > 解决方案 > VB.NET:将datagridview中的文本列转换为图像列并根据文本设置图标

问题描述

我 VB.net,我有一个 datagridview,它显示了数据库查询的一些结果。为了生成它,我执行了一些 sql 查询并将数据写入数据表。然后将该表链接到 dgview

dg_data.DataSource = SqlData.DefaultView

现在,一列可以包含某些(固定和预定义)字符,描述使用我的应用程序处理的事件的某种状态。我想用更直观的图标替换这些字符。

我设法使它工作,但仅在包含根据另一列 (11) 代码字符的图像的新的另一列 (12) 中。但是,我想用那个字符替换列并且只显示图像。这就是我所拥有的:

    '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

当然可以在之后使用丢弃第 11 列

  dg_data.Columns(11).Visible = False

但我在这里寻找更优雅的解决方案,例如

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

对我有什么提示吗?提前致谢!

标签: vb.netreplacedatagridviewicons

解决方案


推荐阅读