[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6679
  • Last Modified:

Adding an image to a datagridview DataGridViewImageColumn

Hi Experts

I have a datagridview with columns being add manually. One of the columns needs have an image which you click on and as a result display a form.

How do I add the image programmatically?

The image at the moment is in a folder called resources one level below the "sln" file and is called "Search16.png"... can I use a .png image??

I used:

Dim btnSelectAsset As New DataGridViewImageColumn

to create the image column for the datagridview...

I will use an event handler to show the form.


If you have better way perhaps using a datagridviewbutton with an image etc I am all ears!




Could someone add a few lines of code to show how to add the image into this unbound column.

Simon
0
si2030
Asked:
si2030
  • 6
  • 6
1 Solution
 
jpaulinoCommented:
You can add tha pnd file to the resources and do something like this:
            Dim btnSelectAsset As New DataGridViewImageColumn
            btnSelectAsset.DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter
            btnSelectAsset.Image = My.Resources.Search16

Open in new window

0
 
si2030Author Commented:
Hi there thanks for getting back...

I am having trouble with the last line.

All I have is a folder I created and named "resources" with all my images in it one of which is search16.png.

Its not a resources file.
0
 
jpaulinoCommented:
Go to My Project - Resources Tab and add your image. You will be able then to use My namespace like the example above.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
si2030Author Commented:
I must be missing something.  I added the resource as a png file and it worked in that there were no errors but I am gettng an "X" where the picture is suppose to go.

 I added an existing file in this case... my initial image.

I then created the image in a bmp and then a gif format still with the same results...

Not sure why the image is not getting through...




 With imgSelectAsset
 
     .DefaultCellStyle.Alignment =  DataGridViewContentAlignment.TopCenter
     .Image = My.Resources.Search16
 
        End With
 
'Add the reference button column.
     .Columns.Add(imgSelectAsset)

Open in new window

0
 
jpaulinoCommented:
Try with another image just to test if that is valid
0
 
si2030Author Commented:
Hi there,

Yes I did that.  Still get the red cross. I created the new image from the IDE this time.

I have included all the code for the subroutine and a picutre of the DGV and a picture of the resources.
Public Sub initaliseDGV()
 
        Presenter.loadAccountDataGridViewCombbox()
        Presenter.loadTypeDataGridViewCombbox()
 
        'Create the DGVComboBoxes.
        Dim accountComboBox As New DataGridViewComboBoxColumn
        Dim typeComboBox As New DataGridViewComboBoxColumn
 
        'Create the DGVButtons.
        'Dim btnSelectAsset As New DataGridViewButtonColumn
 
        'Dim imgSelectAsset As New DataGridViewImageColumn
        'imgSelectAsset.Image= Resources.
        
        Dim imgSelectAsset As New DataGridViewImageColumn
 
        'Create the binding sources.
        Dim BSAccountComboBox As New BindingSource
        Dim BSTypeComboBox As New BindingSource
 
        With accountComboBox
 
            .DataSource = BSAccountComboBox
            .HeaderText = "Account"
            .DropDownWidth = accComboBoxDropDwnWidth
            .Width = 70
            .FlatStyle = FlatStyle.Flat
            .MaxDropDownItems = 10
 
        End With
 
        With typeComboBox
 
            .DataSource = BSTypeComboBox
            .HeaderText = "Type"
            .DropDownWidth = typeComboBoxDropDwnWidth
            .Width = 80
            .FlatStyle = FlatStyle.Flat
            .MaxDropDownItems = 2
 
        End With
 
        With imgSelectAsset
 
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter
            .Image = My.Resources.Image1
 
        End With
 
        With dgvCreditorInvoiceDetail
 
            'Add the accountComboBox column to the DataGridView control.
            .Columns.Add(accountComboBox)
 
            'Add the Detail column.
            .Columns.Add("detail", "Detail")
            .Columns("detail").Width = 250
 
            'Add the Reference column.
            .Columns.Add("ref", "Ref")
            .Columns("ref").Width = 70
 
            'Add the reference button column.
            .Columns.Add(imgSelectAsset)
 
            'Add the typeComboBox column to the DataGridView control.
            .Columns.Add(typeComboBox)
 
            'Add the Client column.
            .Columns.Add("client", "Client")
            .Columns("client").Width = 70
 
            'Add the Client column.
            .Columns.Add("amount", "Amount")
            .Columns("amount").Width = 100
 
        End With
 
    End Sub

Open in new window

DGV.jpg
images.jpg
0
 
jpaulinoCommented:
Strange ... if that doesn't work, try this way:
        Dim imgSelectAsset As New DataGridViewImageColumn
        imgSelectAsset.Image = Image.FromFile("c:\path\image.jpg")
        Me.DataGridView1.Columns.Add(imgSelectAsset)

Open in new window

0
 
si2030Author Commented:
Ok tried that. Didnt work. What you have suggested should work... there must be something I have done etc to cause this to fail.

Could it be that I am running Vista x64 and visual basic 2008? Just a thought.

The only other alternative is to add a button that has an image however I dont know how to this...
0
 
jpaulinoCommented:
Ok, then try this way overriding the DefaultNewRowValue
        Dim imgSelectAsset As New DataGridViewImageColumn
        imgSelectAsset.ImageLayout = DataGridViewImageCellLayout.Normal
        imgSelectAsset.Image = My.Resources.YourImageName
        imgSelectAsset.CellTemplate = New DefaultImageCell
 
        Me.DataGridView1.Columns.Add(imgSelectAsset)
 
 
' In the end of your code or in a new class
Class DefaultImageCell
    Inherits DataGridViewImageCell
 
    Public Overrides ReadOnly Property DefaultNewRowValue() As Object
        Get
            Return My.Resources.YourImageName
        End Get
    End Property
 
End Class

Open in new window

0
 
si2030Author Commented:
Yes that worked.  Could you tell me why (and why the normal approach didnt) and also, perhaps, how in a few words... I will accept the solution no matter what as its worked... just curious...
0
 
jpaulinoCommented:

I don't know if it's a bug or not, but sometimes the cell is created using the default values and doesn't refresh the images. It works fine if you have any refresh to the grid, like a datasource.

Consider this as an workaround!

0
 
si2030Author Commented:
Excellent.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now