PaulEll
asked on
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).DefaultCellSty le.BackIma ge = 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
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).DefaultCellSty
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
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).Cell s(0).Value = My.Resources.Warning
'Image from resources
dataGridView1.Rows(0).Cell s(0).Value = new Bitmap("c:\img.png")
and so on.
Hope this helps
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).Cell
'Image from resources
dataGridView1.Rows(0).Cell
and so on.
Hope this helps
you can do it like following
Dim imgColumn1 As New DataGridViewImageColumn(Fa lse)
imgColumn1.Name = "NewImageColumn"
imgColumn1.ValuesAreIcons = False
imgColumn1.ImageLayout = DataGridViewImageCellLayou t.Stretch
imgColumn1.Resizable = DataGridViewTriState.False
imgColumn1.Width = 65
objfrm.dgvview.Columns.Ins ert(0, imgColumn1)
Dim imgColumn1 As New DataGridViewImageColumn(Fa
imgColumn1.Name = "NewImageColumn"
imgColumn1.ValuesAreIcons = False
imgColumn1.ImageLayout = DataGridViewImageCellLayou
imgColumn1.Resizable = DataGridViewTriState.False
imgColumn1.Width = 65
objfrm.dgvview.Columns.Ins
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
Thanks for the reply.
I'm using VB.net so I can't use div tags as this is for the web.
Sorry
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
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
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
either you have to make image with text or you have to use backcolor insted of image in datagridview cell
ASKER
trideep,
Thanks for the reply.
I see what your saying, I was just trying to work out if a dg grid cell could have a background image.
I can change the colour of a cell and I can add an image column, I just can't see how to give a dg cell a backgorund image.
I was even trying to add a picture box column and then try and add a lable on top.
Can't seem to get it working though.
Thanks Paul
Thanks for the reply.
I see what your saying, I was just trying to work out if a dg grid cell could have a background image.
I can change the colour of a cell and I can add an image column, I just can't see how to give a dg cell a backgorund image.
I was even trying to add a picture box column and then try and add a lable on top.
Can't seem to get it working though.
Thanks Paul
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
CodeCruiser,
Thanks for that.
I will have a look through all of them and try to see if I can get them working on my project.
I'll let you know how I get on.
Thanks
Paul
Thanks for that.
I will have a look through all of them and try to see if I can get them working on my project.
I'll let you know how I get on.
Thanks
Paul
Espavo