How to add an image to ListViewItem or ListView c#

2020-02-13 03:46发布

问题:

I have a listview control, to this control I am adding rows which contain image data, filename, position (its taken at a point in a 4 dimensional matrix), time taken. To make things nice I want to add a 'delete' icon to my fourth column so the user can review the image and decide whether to keep it or not, how do I add an image/icon to that 4th cell in each row?

public void addItemToImageListview(string details, string timestamp)
{
        string reftype = (!exp_image_radiobtn.Checked) ? "Ref" : "Data";
        ListViewItem lvi = new ListViewItem();

        if (details.Contains("[ERROR]"))
        {
            reftype = "[ERROR]";
            details = details.Substring(0, details.IndexOf("[ERROR]"));
        }

        lvi.Text = details;
        lvi.SubItems.Add(reftype);
        lvi.SubItems.Add(timestamp);
        lvi.SubItems.Add(image icon?);

I have looked through Google but a lot of stuff is old...

As Aren asked, this is a winforms app, c#, 3.5.

回答1:

Unfortunately, in WinForms rendering a column as an image gets painful. You have to create an owner-drawn ListView and provide the logic for drawing that column.

Fortunately, there's the open source ObjectListView wrapper, which makes drawing images in ListView subitem columns trivial: