【问题标题】:Image not displaying in datagridview column in vb.net图像未显示在 vb.net 的 datagridview 列中
【发布时间】:2020-04-01 13:48:49
【问题描述】:

我想使用以下代码在 DataGridView 单元格中显示图像:

dgvInventory.Item(7, i).Value = My.Resources.ResourceManager.GetObject("picture")

但不是显示所需的图像,而是在单元格中显示System.Drawing.Bitmap

请注意,DataGridView 表是在运行时创建的,所以我知道我应该将列属性更改为 DataGridViewImageColumn,但我就是不知道怎么做。

提前致谢

我真的需要这方面的帮助。

以下是完整代码

con.Open()
tables.Clear()
dgvInventory.DataSource = tables
dgvInventory.DataSource = Nothing

sql = "SELECT ItemID, itemname, status, ''  FROM [" & InventoryTable & "]"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, InventoryTable)
con.Close() 

'This Loads Records into DataGrid
Dim view As New DataView(tables(0))
source1.DataSource = view
dgvInventory.DataSource = view
dgvInventory.AllowUserToAddRows = False

For i = 0 To dgvInventory.RowCount - 1
   If dgvInventory.Item(2, i).Value = 1 then
     dgvInventory.Item(3, i).Value = My.Resources.ResourceManager.GetObject("picture")
   Else
     dgvInventory.Item(3, i).Value = My.Resources.ResourceManager.GetObject("picture1")
   End If
Next

dgvInventory.Columns(0).HeaderText = "ID"
dgvInventory.Columns(1).HeaderText = "Item / Product / Service"
dgvInventory.Columns(2).HeaderText = "Status"
dgvInventory.Columns(3).HeaderText = "Icon"

dgvInventory.Columns(0).Width = 100
dgvInventory.Columns(1).Width = 300
dgvInventory.Columns(2).Width = 100
dgvInventory.Columns(3).Width = 100

dgvInventory.ClearSelection()

【问题讨论】:

  • 发布您用于生成 DataGridView 的代码及其数据源的性质。由于您可以创建一个新的DataGridViewTextBoxColumn,您也可以创建一个新的DataGridViewImageColumn,初始化方法不会改变(对于默认对象)。顺便说一句,当您从资源生成图像时,将图像分配给位图对象,然后将位图分配给控件的属性。在不再需要时(当您更改控件的属性或父窗体关闭时)处理这些位图对象。
  • 补充 Jimi 的评论,当你创建新的DataGridViewImageColumn 时,将其设置为DefaultCellStyle.NullValue = Nothing,并处理DGVRowsAdded 事件,为每一行设置所需的图像。此外,如果您想摆脱最后一行的默认null 图像,请在同一事件中检查If e.RowIndex = 0 Then ... 将图像单元格的值设置为Nothing。祝你好运。
  • @JQSOFT 好吧,您可以使用自定义DataGridViewImageCell 设置DataGridViewImageColumn.CellTemplate,您可以在其中覆盖DefaultNewRowValue,将其设置为new Bitmap(1,1)(当然,也可以选择自定义位图)在 DataGridViewImageCell 构造函数中(因为它是一个 getter-only 属性)。
  • 我已编辑问题以按要求显示代码
  • 请注意:不是显示基于条件的所需图像(图片/图片1),而是在单元格中写入System.Drawing.Bitmap

标签: .net vb.net winforms datagridview datagridviewimagecolumn


【解决方案1】:

如果你得到类似的东西:

这是因为您将获得 ToString 函数返回的 Bitmap 类型实例的返回值,该实例将显示在默认的 DataGridViewTextBoxCell 中。请注意,图标不是您的DataTable 的一部分,它只是您资源中的图像。

改为在设置 DGV 的 DataSource 属性后添加一个新的 DataGridViewImageColumn。请考虑以下示例:

'Class level variables
Private bmp1 As New Bitmap(My.Resources.picture)
Private bmp2 As New Bitmap(My.Resources.picture1)

在调用和显示数据的方法中,将其替换为您的实际数据源。

Dim dt As New DataTable
dt.Columns.Add(New DataColumn("ID"))
dt.Columns.Add(New DataColumn("Item"))
dt.Columns.Add(New DataColumn("Status"))

For i As Integer = 1 To 10
    dt.Rows.Add(i, $"Item {i}", $"Status {i}")
Next

With dgvInventory
    .Clear()
    .DataSource = Nothing
    .DataSource = dt
    .Columns("Item").HeaderText = "Item / Product / Service"
End With

Dim imageColIndex As Integer = dgvInventory.Columns.Count
Dim imgCol As New DataGridViewImageColumn(False) With {
    .CellTemplate = New DataGridViewImageCell() With {
        .Style = New DataGridViewCellStyle() With {
            .Alignment = DataGridViewContentAlignment.MiddleCenter
        }
    },
    .DisplayIndex = imageColIndex,
    .Image = Nothing,
    .Name = "Image",
    .HeaderText = "Image",
    .Width = CInt(dgvInventory.RowTemplate.Height * 1.5),
    .DefaultCellStyle = New DataGridViewCellStyle() With {.NullValue = Nothing}
}

AddHandler dgvInventory.CellFormatting,
    Sub(obj, arg)
        If arg.ColumnIndex = imageColIndex Then
            arg.Value = If(arg.RowIndex Mod 2 = 0, bmp1, bmp2)
        End If
    End Sub

dgvInventory.Columns.Insert(imageColIndex, imgCol)

其中imageColIndex 是图像列的索引。

不要忘记在 From.Closing 事件中清理:

bmp1?.Dispose()
bmp2?.Dispose()

你会得到类似的东西:

请注意,您不需要在设计时添加列,您可以在绑定DataTableDataViewBindingSource、..等后编辑列属性。到您的 DGV。

在您的代码中实现:

tables.Clear()
con.Open()
sql = "SELECT ItemID, itemname, status, ''  FROM [" & InventoryTable & "]"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, InventoryTable)
con.Close()
con.Dispose()

Dim view As New DataView(tables(0))
source1.DataSource = view

With dgvInventory
    .Columns.Clear()
    .DataSource = Nothing
    .DataSource = view
    .Columns(0).HeaderText = "ID"
    .Columns(0).Width = 100
    .Columns(1).HeaderText = "Item / Product / Service"
    .Columns(1).Width = 300
    .Columns(2).HeaderText = "Status"
    .Columns(2).Width = 100
End With

Dim imageColIndex As Integer = dgvInventory.Columns.Count
Dim imgCol As New DataGridViewImageColumn(False) With {
    .CellTemplate = New DataGridViewImageCell() With {
    .Style = New DataGridViewCellStyle() With {
    .Alignment = DataGridViewContentAlignment.MiddleCenter
}
},
.DisplayIndex = imageColIndex,
.Image = Nothing,
.Name = "Icon",
.HeaderText = "Icon",
.Width = CInt(dgvInventory.RowTemplate.Height * 1.5),
.DefaultCellStyle = New DataGridViewCellStyle() With {.NullValue = Nothing}
}

AddHandler dgvInventory.CellFormatting,
    Sub(obj, arg)
        If arg.ColumnIndex = imageColIndex Then
            Dim status As Integer = Convert.ToInt32(
            DirectCast(obj, DataGridView).Item(2, arg.RowIndex).Value)

            arg.Value = If(status = 1, bmp1, bmp2)
        End If
    End Sub

dgvInventory.Columns.Insert(imageColIndex, imgCol)
dgvInventory.ClearSelection()

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2015-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多