.net - 图像未显示在 vb.net 的 datagridview 列中
问题描述
我想使用以下代码在 DataGridView 单元格中显示图像:
dgvInventory.Item(7, i).Value = My.Resources.ResourceManager.GetObject("picture")
但不是显示所需的图像,而是显示System.Drawing.Bitmap
在单元格中。
请注意,DataGridView 表是在运行时创建的,所以我知道我应该将 column 属性更改为 an,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()
解决方案
如果你得到类似的东西:
那是因为您正在获取ToString
函数返回的类型实例的返回值,该Bitmap
类型实例将显示在 defaultDataGridViewTextBoxCell
中。请注意,图标不是您的一部分DataTable
,它只是您资源中的图像。
DataGridViewImageColumn
而是在设置DataSource
DGV 的属性后添加一个新的。请考虑以下示例:
'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()
你会得到类似的东西:
请注意,您不需要在设计时添加列,您可以在绑定 a DataTable
, DataView
, BindingSource
, ..etc 后编辑列属性。到您的 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()
推荐阅读
- javascript - Axios post request not sent to external url: TypeError: ns.GetCommandSrc is not a function
- java - Java.IO.io 异常处理
- java - Cucumber:在执行第一个脚本“警告:在类路径中找不到功能:/Runner”时显示
- javascript - 在 nodejs 中将货币符号写入文件的问题
- azure-sql-database - 将 ssis 2008 c# 架构升级到 2016 for azure
- vue.js - 如何在测试模式而不是生产模式下运行赛普拉斯?
- javascript - 在另一个屏幕中更改状态也会更改 DrawerNavigation 的状态
- azure-devops - Azure DevOps - 迭代更改
- sql - 在使用分区创建新列时,我得到了?在新列数据中
- excel - 以月为单位计算日期之间的差异,并根据定义的截止日期 MS EXCEL