vb.net datagrid - How to set a background image for a cell
Hi there,
I am using VB.net and I am trying to add a background image to a datagrid cell.
This is either impossible or I have missed something very simple.
Hopefully somebody here can point me in the right direction.
The code below just sets the background colour depending on a value.
What I really want to be able to do is
.Columns(0).DefaultCellStyle.BackImage = MyImage.
Has any one done this ? Can you point me in the right direction.
I can only use VB.net for the solution.
Thanks
Paul
DataGrid1.DataSource = MyCalendarTable With DataGrid1 .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill .RowHeadersVisible = False If MyDay1 = "Sun" Then .Columns(0).DefaultCellStyle.BackColor = Color.Gainsboro End If If MyDay2 = "Sun" Then .Columns(1).DefaultCellStyle.BackColor = Color.Gainsboro End If If MyDay3 = "Sun" Then .Columns(2).DefaultCellStyle.BackColor = Color.Gainsboro End If If MyDay4 = "Sun" Then .Columns(3).DefaultCellStyle.BackColor = Color.Gainsboro End If If MyDay5 = "Sun" Then .Columns(4).DefaultCellStyle.BackColor = Color.Gainsboro End If If MyDay6 = "Sun" Then .Columns(5).DefaultCellStyle.BackColor = Color.Gainsboro End If If MyDay7 = "Sun" Then .Columns(6).DefaultCellStyle.BackColor = Color.Gainsboro End If .Columns(3).DefaultCellStyle.BackColor = Color.LightCyan End With
I've never tried this, but I'd use template fields in the datagrid, and then drop <div>'s with background images into the <div>'s... Espavo
bklmnsh
Your DataGridView column has to be of type DataGridViewImageColumn.
Then you can just assign an image as a value of a cell.
So assuming your first column is of type DataGridViewImageColumn, we can write the following code:
'Image from resources
dataGridView1.Rows(0).Cells(0).Value = My.Resources.Warning
'Image from resources
dataGridView1.Rows(0).Cells(0).Value = new Bitmap("c:\img.png")
and so on.
Hope this helps
Trideep Patel
you can do it like following
Dim imgColumn1 As New DataGridViewImageColumn(False)
imgColumn1.Name = "NewImageColumn"
imgColumn1.ValuesAreIcons = False
imgColumn1.ImageLayout = DataGridViewImageCellLayout.Stretch
imgColumn1.Resizable = DataGridViewTriState.False
imgColumn1.Width = 65
objfrm.dgvview.Columns.Insert(0, imgColumn1)
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
PaulEll
ASKER
Espavo,
Thanks for the reply.
I'm using VB.net so I can't use div tags as this is for the web.
Sorry
PaulEll
ASKER
BKLMNSH, trideep
Thanks for the reply.
I'll have a look at your suggestions but I am not sure it will do what I want.
To explain:
I want to put a background image behind a cell.
I then want to put text over the image.
The idea being:
If Myvar = 1 then
code to use background image 1
else
code to use default background image
end if
The text has to be able to go over the top of the image.
I hope that is now clearer
Thanks for your speedy replies
Trideep Patel
I think you can not write anything on image column
either you have to make image with text or you have to use backcolor insted of image in datagridview cell
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
Espavo