This is a migrated thread and some comments may be shown as answers.

Image in a command column

5 Answers 231 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Malcolm
Top achievements
Rank 1
Malcolm asked on 24 Jan 2011, 06:02 PM
Hi,

I've looked at a few examples of this but they don't appear to work. I'm getting an error when actually assigning the image. The error is Exception has been thrown by the target of an invocation.

The code that I'm trying is:
Private Sub rgvItems_CellFormatting(ByVal sender As Object, ByVal e As CellFormattingEventArgs) Handles rgvItems.CellFormatting
    If TypeOf e.CellElement.ColumnInfo Is GridViewCommandColumn AndAlso Not (TypeOf e.CellElement.RowElement Is GridTableHeaderRowElement) Then
        Dim column As GridViewCommandColumn = DirectCast(e.CellElement.ColumnInfo, GridViewCommandColumn)
        If column.Name = "PLButton" Then
            Dim element As RadButtonElement = DirectCast(e.CellElement.Children(0), RadButtonElement)
            element.DisplayStyle = Telerik.WinControls.DisplayStyle.Image
            ' This is an example of course
            element.Image = ImageList.Images(0)
            'e.CellElement.StringAlignment = StringAlignment.Center
            'Me.ApplyThemeToElement(element, "ControlDefault")
        End If
    End If
End Sub
The line where it seems to fail is element.image = imagelist.images(0). The imagelist is an image list surprisingly enough and it has images in it. They are being used elsewhere to populate an image column in the grid view.

Is there anything obvious stands out?

Thanks

5 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 24 Jan 2011, 06:14 PM
Hello,

I've just tried the following in an existing grid, inside the CellFormatting event

If TypeOf e.CellElement.ColumnInfo Is GridViewCommandColumn AndAlso Not (TypeOf e.CellElement.RowElement Is GridTableHeaderRowElement) Then
    Dim column As GridViewCommandColumn = DirectCast(e.CellElement.ColumnInfo, GridViewCommandColumn)
    If column.Name = "Button" Then
        Dim element As RadButtonElement = DirectCast(e.CellElement.Children(0), RadButtonElement)
        element.DisplayStyle = Telerik.WinControls.DisplayStyle.Image
        ' This is an example of course 
        element.Image = Image.FromFile("C:\Users\Richard\Pictures\img.gif")
        'e.CellElement.StringAlignment = StringAlignment.Center
        'Me.ApplyThemeToElement(element, "ControlDefault")
    End If
End If

and it all worked fine. Please could you try this (taking an Image from file from yuor own machine) and see if that works please.
Thanks
Richard

0
Malcolm
Top achievements
Rank 1
answered on 25 Jan 2011, 09:57 AM
Ah ha! It would seem that I can load from file, including the image I was trying to load (a PNG). But why not from the image list? I'm already using that to populate images in an image column.

Seems a weird one really.

Cheers!
0
Richard Slade
Top achievements
Rank 2
answered on 25 Jan 2011, 10:16 AM
Hi Stephen,

I haven't been able to replicate your issue at the moment. Please can you post here a small sample and i'll be happy to take a look at it for you
Richard
0
Malcolm
Top achievements
Rank 1
answered on 25 Jan 2011, 10:30 AM
Richard, I'm a complete idiot. My imagelist was called StatusImages. D'oh. Fixed now.

Colour me exceptionally embarresed!
0
Richard Slade
Top achievements
Rank 2
answered on 25 Jan 2011, 10:34 AM
Not to worry. Happens to us all!
All the best
Richard
Tags
GridView
Asked by
Malcolm
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Malcolm
Top achievements
Rank 1
Share this question
or