图片未显示在vb.net的datagridview列中 [英] Image not displaying in datagridview column in vb.net

查看:104
本文介绍了图片未显示在vb.net的datagridview列中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following code that I want to use to display an image in a DataGridView cell:

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

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

But instead of displaying the required image, it shows System.Drawing.Bitmap in the cell.

请注意,DataGridView表是在运行时创建的,因此我知道我应该将column属性更改为DataGridViewImageColumn,但我只是不知道如何操作.

Please note that the DataGridView table was created at run time, so I know I am supposed to change the column property to be an DataGridViewImageColumn but I just could not figure out how to.

预先感谢

请我真的需要帮助.

下面是完整的代码

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()

推荐答案

如果您得到类似的东西:

If you are getting something like:

那是因为您正在获取ToString函数返回的值,该值将在默认的DataGridViewTextBoxCell中显示Bitmap类型的实例.请注意,图标不是您的DataTable的一部分,而只是来自您资源的图像.

That's because you are getting what the ToString function returns for an instance of a Bitmap type to be displayed in a default DataGridViewTextBoxCell. Note that, the Icon is not a part of your DataTable, its just an image from your resources.

相反,在设置DGV的DataSource属性后,添加一个新的DataGridViewImageColumn.请考虑以下示例:

Instead, add a new DataGridViewImageColumn after setting the DataSource property of the DGV. Please consider the following example:

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

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

In a method that calls and displays the data replace this with your actual data source.

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事件中进行清理:

Don't forget to clean up in the From.Closing event:

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

,您将得到类似的东西:

and you will get something like:

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

Note that, you don't need to add columns at design time, you can edit the columns properties after binding a DataTable, DataView, BindingSource, ..etc. to your 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()

这篇关于图片未显示在vb.net的datagridview列中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆