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

Add Image

4 Answers 79 Views
This is a migrated thread and some comments may be shown as answers.
Javier Gonzalez de Aragon
Top achievements
Rank 2
Javier Gonzalez de Aragon asked on 22 Nov 2012, 01:22 AM
Hi.

I have a Listview control populated using a Datadapter. I want to display an image from an ImageList control. The index is based on a value in a column. How can I achieve this?

Thanks,

Javier

4 Answers, 1 is accepted

Sort by
0
Javier Gonzalez de Aragon
Top achievements
Rank 2
answered on 23 Nov 2012, 08:07 PM
I have found the way.

Private Sub lvLista_CellFormatting(sender As Object, e As Telerik.WinControls.UI.ListViewCellFormattingEventArgs) Handles lvLista.CellFormatting
 
    If TypeOf e.CellElement Is DetailListViewHeaderCellElement Then
        Return
    End If
 
    Dim dataCell As DetailListViewDataCellElement = e.CellElement
 
    Select Case e.CellElement.Data.HeaderText
        Case "Número"
            e.CellElement.TextAlignment = ContentAlignment.MiddleCenter
            dataCell.Text = Format(dataCell.Row(e.CellElement.Data.Name), "000000")
            e.CellElement.ImageIndex = dataCell.Row("Imagen")
        Case "Fecha"
            e.CellElement.TextAlignment = ContentAlignment.MiddleCenter
            dataCell.Text = Format(dataCell.Row(e.CellElement.Data.Name), "dd/MM/yyyy")
        Case Else
            e.CellElement.TextAlignment = ContentAlignment.MiddleLeft
    End Select
 
End Sub
0
Plamen
Telerik team
answered on 26 Nov 2012, 01:27 PM
Hi Javier,

Thank you for writing.

While your approach will work and the image will be assigned correctly, I would suggest using the ItemDataBound event to assign your images for better performance. ItemDataBound event is fired when an item is data bound, while the formatting event is constantly fired. 

If you need more information about the control and its functionalities, feel free to take a look at our online documentation: http://www.telerik.com/help/winforms/listview-getting-started.html.

Should you have any further questions, do not hesitate to contact us.

Regards,
Plamen
the Telerik team
Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
0
Javier Gonzalez de Aragon
Top achievements
Rank 2
answered on 29 Nov 2012, 08:56 PM
I had previously tried that approach, but I couldn't find a way to retrieve the values from the columns I needed. Any ideas?

Thanks.
0
Accepted
Plamen
Telerik team
answered on 30 Nov 2012, 03:16 PM
Hi Javier,

Thank you for writing back.

In order to retrieve the desired value from the column, you can use the DataBoundItem property of the Item, which is in fact the DataRowView of this item. Here is an example:
Private Sub radListView1_ItemDataBound(sender As Object, e As ListViewItemEventArgs)
    Dim rowView As DataRowView = DirectCast(e.Item.DataBoundItem, DataRowView)
    e.Item.ImageIndex = CInt(rowView.Row("Imagen"))
End Sub

I hope that you find this information helpful. Let us know if you need anything else.

Regards,
Plamen
the Telerik team
Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
Asked by
Javier Gonzalez de Aragon
Top achievements
Rank 2
Answers by
Javier Gonzalez de Aragon
Top achievements
Rank 2
Plamen
Telerik team
Share this question
or