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")
But instead of displaying the required image, it shows System.Drawing.Bitmap in the cell.
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.
Thanks in advance
Please I really need help with this.
Below is the Complete code
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:
That's because you are getting what the
ToStringfunction returns for an instance of aBitmaptype to be displayed in a defaultDataGridViewTextBoxCell. Note that the icon is not a part of yourDataTable, it's just an image from your resources.Instead, add a new
DataGridViewImageColumnafter setting theDataSourceproperty of the DGV. Please consider the following example:In a method that calls and displays the data, replace this with your actual data source.
Where
imageColIndexis the index of the image column.Don't forget to clean up in the Form.Closing event:
and you will get something like:
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.Implementing that in your code: