Search code examples
c#winformsobjectlistview

Show bound byte array as an image in DataListView


I'm using the ObjectListView controls set, specifically the DataListView. Here is my code.

Datasource of the control:

dataListView1.DataSource = this.ViewModel.DataSet;

where DataSet is populated by:

this.ViewModel.DataSet = HSGlobals.ctx.Entities<Product>().All()
                        .Where(p => productIdList.Contains(p.Id)).Select(p => new
                        {
                            Nazwa = p.Name,
                            Kod = p.Code,
                            Dostępność = p.AvaibleQuantity,
                            Typ = p.ProductType.Name,
                            Miara = p.MeasureUnit.Name,
                            Grafika = p.ProductImage.Image
                        }).ToList();

Grafika is a byte array - an image obtained from database.

When dataListView is displayed, the Grafika column shows only type of this field - System.byte[].

What I have to do to display just an image?

I've already tried ImageGetter and Renderer properties but no success.


Solution

  • Your initial approach should work, if you attach an instance of the BrightIdeasSoftware.ImageRenderer to the Renderer of the column.

    For the ImageRenderer, the image can be sourced from:

    • a byte-array (normally when the image to be shown is stored as a value in a database)
    • an int, which is treated as an index into the image list
    • a string, which is treated first as a file name, and failing that as an index into the image list
    • an ICollection of ints or strings, which will be drawn as consecutive images

    Assuming your byte-stream contains valid data to create a Bitmap this should do.

    EDIT: Also make sure that the OwnerDraw property of the OLV is set to true, IIRC this is a requirement that custom images will be drawn.