Setting images in DataListView (Part of ObjectListView)

时间:2015-12-14 18:11:09

标签: vb.net 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'

1 个答案:

答案 0 :(得分:2)

首先,将列表的OwnerDraw属性设置为true:

yourList.OwnerDraw = True 

然后将您的列表链接到ImageList:

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

然后,您应该在ObjectListView列上设置图像getter委托,例如:

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

委托函数将索引返回到图像列表中,或者,由于ObjectListView是所有者绘制的,因此委托可以返回图像。