Add Image

5 posts, 1 answers
  1. Javier Gonzalez de Aragon
    Javier Gonzalez de Aragon avatar
    35 posts
    Member since:
    Mar 2010

    Posted 21 Nov 2012 Link to this post

    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
  2. Javier Gonzalez de Aragon
    Javier Gonzalez de Aragon avatar
    35 posts
    Member since:
    Mar 2010

    Posted 23 Nov 2012 Link to this post

    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
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Plamen
    Admin
    Plamen avatar
    132 posts

    Posted 26 Nov 2012 Link to this post

    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.
  5. Javier Gonzalez de Aragon
    Javier Gonzalez de Aragon avatar
    35 posts
    Member since:
    Mar 2010

    Posted 29 Nov 2012 Link to this post

    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.
  6. Answer
    Plamen
    Admin
    Plamen avatar
    132 posts

    Posted 30 Nov 2012 Link to this post

    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.
Back to Top
UI for WinForms is Visual Studio 2017 Ready