Thursday, September 21, 2006 8:45 PMHi,
I am trying to add an image to a subitem of my listview , how can I do this?
if not possible, are there any other alternatives?
thanks in advance!
Thursday, September 21, 2006 8:56 PM
By default only the root items have images.
You should however be able to override this by drawing the subitems yourself. Handle the ListView.DrawSubItem event and add your custom drawing code.
Thursday, September 21, 2006 9:00 PMCould you please post some sample code?
Friday, September 22, 2006 1:40 AM
Yeah, look at this thread. It's exactly what you want.
Moreover, I think you will definitely enjoy this sample
Friday, September 22, 2006 2:54 PM
You've got some links to look at apparently (haven't checked them myself), but here is a very simple example. Before this code will have effect, you'll need to set the OwnerDraw property of the ListView to True:
Private Sub ListView1_DrawColumnHeader(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawListViewColumnHeaderEventArgs) Handles ListView1.DrawColumnHeader
e.DrawDefault = True
Private Sub ListView1_DrawSubItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawListViewSubItemEventArgs) Handles ListView1.DrawSubItem
If Not e.Item.SubItems(0) Is e.SubItem Then
e.DrawDefault = False
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)
e.DrawDefault = True
You may want to extend this further for the various Item states.
Wednesday, January 14, 2009 4:13 PM
it is possible to set an icon into for a subitem since version 4.70 (thinking its in there since win2k)
LVS_EX_SUBITEMIMAGES is the trick
.... | LVS_EX_SUBITEMIMAGES
then set for the corresponding subitem.
Wednesday, May 18, 2011 10:13 AM
This seems working fine except one small problem. If you select the row in list view, all columns are highlighted in LightBlue (the default window colour for selection) except the column in which you draw image & string.
I've been messed around with this problem and didn't get any satisfactory solution. Can anyone help please.
Wednesday, May 18, 2011 9:53 PM
You have to examine the e.ItemState and if it is selected, draw the highlight yourself. This typically means drawing a rectangle in the selection color first, then changing the font color and drawing on top of the selection rectangle, or performing your drawing first and then putting a semitransparent selection rectangle on top. Depends on the visual effect you want.
Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
- Proposed As Answer by nkvls Thursday, May 19, 2011 3:21 PM
Thursday, May 19, 2011 3:29 PM
Hi, thanks for the reply. You are right. I did more-or-less the same and won the bug. This is I really did...
protected void ListViewDrawSubItem(object sender, DrawListViewSubItemEventArgs e)
if (e.ColumnIndex == 1)
//This allows window to draw background otherwise your column would be blank in anyway
//Draw you graphics here
e.Graphics.DrawImage(img, new System.Drawing.RectangleF(e.Item.Position.X + Columns.Width - 10, e.Item.Position.Y, img.Width*(fScale), img.Height*(fScale)));
//Draw your text here
//This is something really important if you want to set the highlighted color (windows blue) to the ListViewSubItem
e.Item.UseItemStyleForSubItems = false;
e.Item.SubItems.BackColor = System.Drawing.Color.FromArgb(51, 153, 255);
e.Item.SubItems.ForeColor = System.Drawing.Color.FromArgb(255, 255, 255);
e.Item.SubItems.BackColor = System.Drawing.Color.FromArgb(229, 229, 229);
e.Item.SubItems.ForeColor = System.Drawing.Color.FromArgb(0, 0, 0);
This solved my problem and highlight the grid column as exactly as other columns.
- Proposed As Answer by nkvls Thursday, May 19, 2011 3:29 PM