Pankaj Mishra
Pankaj Mishra

Reputation: 20348

How to Add Image In List View in C#?

I have windows application where data comes via json I have parsed the data and able to show in listview.

Now i want to add image to with in. I have tried lot oh things but this is not working. As per my need.

Please guide how i will show this data in list box.

I am using this code.

listView1.Columns.Add("brand", 100, HorizontalAlignment.Left);
listView1.Columns.Add("rating", 100, HorizontalAlignment.Left);
listView1.Columns.Add("max_price", 100, HorizontalAlignment.Left);
listView1.SmallImageList = imageList1;

foreach (var item in lstItemDetails)
{
    ListViewItem objListViewItem = new ListViewItem(item.image_medium);
    objListViewItem.SubItems.Add(item.brand);

    objListViewItem.SubItems.Add(item.rating);
    objListViewItem.SubItems.Add(item.max_price);

    if (!string.IsNullOrEmpty(item.rating))
    {
        int rating = int.Parse(item.rating);
        objListViewItem.ImageIndex = rating;
    }
    else
    {
        objListViewItem.ImageIndex = 0;
    }

     listView1.Items.Add(objListViewItem);
}

This is showing only last assigned image in all place. start rating image as per item.rating count.

Please suggest me the better way to solve this.

Upvotes: 4

Views: 10962

Answers (1)

Grammarian
Grammarian

Reputation: 6882

If you are doing anything with a ListView, you do yourself a huge favour and use an ObjectListView instead. ObjectListView is a wrapper around a standard .NET ListView which provides methods for just about everything you could want to do, plus fixes for almost all the problems/bugs that ListView has.

For example, it can build your whole ListView -- complete with sorting, grouping, and editing -- by just creating the columns and then calling SetObjects()

enter image description here

Upvotes: 4

Related Questions