Sharrok G
Sharrok G

Reputation: 457

How to add an image in listview.subitem

I want to know how to add image in list view sub item.

I am using this code to display text in the sub item.

 double Text = "2452";
 ListViewItem lItem = new ListViewItem();
 lItem.SubItems.Add(Text.ToString());

I want to do something like this

 ListViewItem lItem = new ListViewItem();
 lItem.SubItems.Add(Text.ToString() + "C:\\image.png");

Thanks In Advance.

Upvotes: 5

Views: 19585

Answers (3)

rockyashkumar
rockyashkumar

Reputation: 1332

private void ListView1_DrawColumnHeader(object sender, System.Windows.Forms.DrawListViewColumnHeaderEventArgs e) {
    e.DrawDefault = true;
}

private void ListView1_DrawSubItem(object sender, System.Windows.Forms.DrawListViewSubItemEventArgs e) {
    if (!(e.Item.SubItems(0) == e.SubItem)) {
        e.DrawDefault = false;
        e.DrawBackground();
        e.Graphics.DrawImage(My.Resources.Image1, e.SubItem.Bounds.Location);
        e.Graphics.DrawString(e.SubItem.Text, e.SubItem.Font, new SolidBrush(e.SubItem.ForeColor), (e.SubItem.Bounds.Location.X + My.Resources.Image1.Width), e.SubItem.Bounds.Location.Y);
    }
    else {
        e.DrawDefault = true;
    }
}

Upvotes: 12

Paul Hennessey
Paul Hennessey

Reputation: 203

You can do this by implementing your own HttpHandler for the image. This is a .ashx file that implements the IHttpHandler interface.

So if you built one called ImageHandler you could use it declaratively in your ListView, something like this:

<asp:ListView 
     ID="ImageListView" 
     runat="server"
     DataKeyNames="Id">
        <ItemTemplate>        
            <img id="img1" src='<%#"~/ImageHandler.ashx?Id=" + Eval("Id") %>' />
        </ItemTemplate>
</asp:ListView>

Upvotes: -4

Miguel Angelo
Miguel Angelo

Reputation: 24192

ListView does not support this.

I found another answer, suggesting that you switch it with DataGridView.

How can I set an icon for a ListViewSubItem?

You could of course, do custom painting of the ListView if you wish, but I must say that things will get very complicated this way.

Upvotes: 4

Related Questions