Link to home
Start Free TrialLog in
Avatar of PaulEll
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).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

Open in new window

Avatar of Espavo
Espavo
Flag of South Africa image

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
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
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)
     
Avatar of PaulEll
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
Avatar of 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






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
Avatar of PaulEll

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
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PaulEll

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