Search code examples
vb.netobjectlistview

Setting images in DataListView (Part of ObjectListView)


I've just started using the DataListView (Part of ObjectListView package). I'm binding a datatable to the DataListView but now I want to add images to the data based on certain criteria. I've spent hours reading the help files (http://objectlistview.sourceforge.net/cs/recipes.html#how-do-i-bind-a-dataset-to-an-objectlistview) but they all seem to reference 'ObjectListView' instead of 'DataListView'.

I've read a lot about the imagegetter but I don't think that applies when you're binding to a datatable (if so, how?).

Should I loop through each line and manually add the image based on the criteria? If so, could you help me get this started?

Here is an example from their webpage. See how they have the image in the first column (and other) based on the data? I'd like to do that with the DataListView. enter image description here

Thanks in advanced. I'm using VB.net

Now I have this:

    Dim myImages = New ImageList
    myImages.Images.Add(My.Resources.important_High_icon)
    myImages.Images.Add(My.Resources.important_Med_icon)
    myImages.Images.Add(My.Resources.important_Low_icon)
    myDataListView.SmallImageList = myImages

    myDataListView.OwnerDraw = True
    Image_Column.ImageGetter = Function(x As Object) As Integer
                                   Select Case (Important_Column.value)
                                       Case "High"
                                           Return 0
                                       Case "Medium"
                                           Return 1
                                       Case "Low"
                                           Return 2
                                   End Select
                               End Function

How do I reference another column in that row to base the logic off of? In the example above, I'm trying to reference the value in the 'Important_Column'


Solution

  • First, set your list's OwnerDraw property to true:

    yourList.OwnerDraw = True 
    

    Then link your list to an ImageList:

       myImages = New ImageList
       myImages.Images.Add(My.Resources.image_1)
       myImages.Images.Add(My.Resources.image_2)
       yourList.SmallImageList = yourImageList
    

    Then you should set an image getter delegate on your ObjectListView column, for example:

    myOlvColumn.ImageGetter = 
        Function(x As Object) As Integer
            Dim casted As yourColumnRealType = DirectCast(x, yourColumnRealType)
            Return If(yourCondition, 0, 1)
        End Function
    

    The delegate function returns an index into the image list, or, since the ObjectListView is owner-drawn, the delegate can return an Image.