vb.net中的datagridview列中未显示图像
我想使用以下代码在DataGridView单元格中显示图像:vb.net中的datagridview列中未显示图像,.net,vb.net,winforms,datagridview,datagridviewimagecolumn,.net,Vb.net,Winforms,Datagridview,Datagridviewimagecolumn,我想使用以下代码在DataGridView单元格中显示图像: dgvInventory.Item(7, i).Value = My.Resources.ResourceManager.GetObject("picture") 但不是显示所需的图像,而是在单元格中显示System.Drawing.Bitmap 请注意,DataGridView表是在运行时创建的,因此我知道应该将column属性更改为DataGridViewImageColumn,但我就是不知道如何更改 提前谢谢 拜托,我真的需要
dgvInventory.Item(7, i).Value = My.Resources.ResourceManager.GetObject("picture")
但不是显示所需的图像,而是在单元格中显示System.Drawing.Bitmap
请注意,DataGridView表是在运行时创建的,因此我知道应该将column属性更改为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
类型实例,该类型将显示在默认的DataGridViewTextBoxCell
中。请注意,图标不是数据表的一部分,它只是资源中的一个图像
相反,在设置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()
你会得到这样的结果:
请注意,您不需要在设计时添加列,您可以在绑定DataTable
、DataView
、BindingSource
、…等后编辑列属性。给你的DGV
在代码中实现这一点:
tables.Clear()
con.Open()
sql=“从[”&InventoryTable&“]中选择项目ID、项目名称、状态”
da=新的OleDb.OleDbDataAdapter(sql,con)
da.填充(ds,库存表)
con.Close()
con.Dispose()
作为新数据视图的Dim视图(表(0))
source1.DataSource=view
与维尼托利一起
.Columns.Clear()
.DataSource=无
.DataSource=view
.Columns(0).HeaderText=“ID”
.列(0)。宽度=100
.Columns(1).HeaderText=“项目/产品/服务”
.列(1).宽度=300
.Columns(2).HeaderText=“状态”
.列(2).宽度=100
以
Dim imageColIndex为整数=dgvInventory.Columns.Count
将imgCol调整为新的DataGridViewImageColumn(False),并带有{
.CellTemplate=新的DataGridViewImageCell(),带有{
.Style=新的DataGridViewCellStyle(),带有{
.Alignment=DataGridViewContentAlignment.MiddleCenter
}
},
.DisplayIndex=imageColIndex,
.Image=无,
.Name=“图标”,
.HeaderText=“图标”,
.Width=CInt(dgvInventory.RowTemplate.Height*1.5),
.DefaultCellStyle=New DataGridViewCellStyle(),带{.NullValue=Nothing}
}
AddHandler dgvInventory.CellFormatting,
分队(obj、arg)
如果arg.ColumnIndex=imageColIndex,则
Dim状态为Integer=Convert.ToInt32(
DirectCast(obj,DataGridView).Item(2,arg.RowIndex).Value)
参数值=If(状态=1,bmp1,bmp2)
如果结束
端接头
dgvInventory.Columns.Insert(imageColIndex,imgCol)
dgvInventory.ClearSelection()
Post-te您用来生成DataGridView及其数据源的代码。由于可以创建新的DataGridViewTextBoxColumn
,也可以创建新的DataGridViewImageColumn
,因此初始化方法不会更改(对于默认对象)。顺便说一句,从资源生成图像时,将图像分配给位图对象,然后将位图分配给控件的属性。当不再需要这些位图对象时(当您更改控件的属性或父窗体关闭时)。除了Jimi的注释外,当您创建新的DataGridViewImageColumn
时,将其设置为DefaultCellStyle.NullValue=Nothing
,并处理DGV
的RowsAdded
事件,为每行设置所需的图像。另外,如果您想删除最后一行的默认null
图像,在同一事件中,选中如果e.RowIndex=0,则选中。。。将图像单元格的值设置为Nothing
。祝您好运。@JQSOFT很好,您可以使用自定义的DataGridViewImageCell
设置DataGridViewImageColumn.CellTemplate
,在DataGridViewImageCell
构造函数中覆盖DefaultNewRowValue
,将其设置为新位图(1,1)
(当然也可以选择自定义位图)(因为它是一个仅getter属性)。我已编辑了问题以按要求显示代码请注意:它没有显示基于条件的所需图像(picture/picture1),而是在单元格中写入System.Drawing.Bitmap
。注释不用于扩展讨论;此对话已取消。