Asked by:
smallimagelist of listview - how to place an image in the second column

Question
-
Without any control editing I can easily attach an imagelist to a listview and place an image in the first column; however, I need an image from this list to be placed in the second column. How can I do this?Thursday, July 21, 2011 2:25 AM
All replies
-
Why dont you rather use a DataGridView control, rather then ListView? It will be far more easier to do, then putting images into subitems of listView.
But if you insist of using ListView, you must handle the event DrawSubItem, assuming that OwnerDraw property is set to true. Checkl here.
MitjaThursday, July 21, 2011 4:39 AM -
hi dhalberger
using list view you can handle the code in <ItemTemplate> tag to display more than one picture in each row. for instance i have used this code to show multiple pictures in a row :
i have used a SQL data source . it will show images after each other in a row. beside yo can use Datalist control which has Columns property .<asp:ListView ID="ListView2" runat="server" ViewStateMode="Disabled"> <ItemTemplate> <a href='<%# "/UploadFile/"+Eval("PRJPICFile") %>' target="_blank"><img alt="" width="100px" src='<%# "/UploadFile/"+Eval("PRJPICFile") %>' /></a> </ItemTemplate> </asp:ListView>
Thursday, July 21, 2011 4:55 AM -
I took a look at it and that control looks preferable. But how exactly can I add an image to a particular cell in such a grid? I've tried this, to no avail:
int iconWidth = 24;
int iconHeight = 24;
Bitmap tempimg = new Bitmap("C:\\Client\\images\\avatars\\1.jpg");
Bitmap img = new Bitmap(tempimg, new Size(iconWidth, iconHeight));
tempimg = null;friendListdgv.Rows[1].Height = img.Height + 5; // cushion image
friendListdgv.Rows[1].Cells[0].Value = img;In that particular cell I get the text "System.Drawing.Bitmap"
Thursday, July 21, 2011 6:14 AM -
Hi dhalberger,
Your code is correct but DataGridViewImageColumn of dgv was not properly set.
You can set it programmatically or using dgv designer wizard.
So programmatically setting DataGridViewImageColumn:
private void Form1_Load(object sender, EventArgs e) { DataGridViewImageColumn img = new DataGridViewImageColumn(); DataGridViewTextBoxColumn description = new DataGridViewTextBoxColumn(); friendListdgv.Columns.Add(description); //col 0 friendListdgv.Columns.Add(img); // col 1 description.HeaderText = "Description"; img.HeaderText = "Image"; } private void btnShowImage_Click(object sender, EventArgs e) { int iconWidth = 24; int iconHeight = 24; Bitmap tempimg = new Bitmap(@"C:\Client\images\avatars\1.jpg"); Bitmap img = new Bitmap(tempimg, new Size(iconWidth, iconHeight)); tempimg = null; friendListdgv.Rows[0].Height = img.Height + 5; // cushion image friendListdgv.Rows[0].Cells[1].Value = img; }
Anyway, I tried this also to a listview but it is so hard to manipulate that control. Actually, I have here some links on how to add image at listview:
http://www.codeproject.com/KB/list/OAKListView.aspx
http://www.codeproject.com/KB/list/EXListView.aspx?df=100&forumid=266091&exp=0&select=1427247
Hope it helps.
Hardz
Friday, July 22, 2011 9:11 AM