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.
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.
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.
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
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