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

Visual Basic ClassicVisual Basic.NET

Avatar of undefined
Last Comment
PaulEll

8/22/2022 - Mon
Espavo

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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
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
Nasir Razzaq

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
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.
ask a question
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