I want to change the color of the group header text in listview which separates all the items into various groups, there is no such option to do that directly because no such property exists.

As far as i can think of it can be achieved via overriding the paint event but i have not done any overriding of paint events on windows controls so far.

Anyone knows how to do it ?

I would be very thankful if someone can suggest a solution.

Member #1094511 commented: how to use the listview drawitem event +0

Dani AI

Generated

Short answer: the WinForms ListView does not expose group headers to owner draw, and Windows’ common controls do not support setting a custom text color for group headers. The LVGROUPMETRICS flags for header/footer text color are marked “Not implemented,” so calls to change them are ignored on supported desktop Windows versions. In other words, you cannot recolor a real ListView group header via .NET or documented Win32 APIs. See Microsoft’s notes on LVGROUPMETRICS and OwnerDraw scope. LVGROUPMETRICS and ListView.OwnerDraw.

Building on ’s tip: OwnerDraw lets you customize items, subitems, and column headers only; it does not include ListViewGroup headers. , you can mix default and custom drawing by setting e.DrawDefault = True in the handlers you do not want to take over (as showed), but that still will not touch group headers. The OS theme draws those.

A practical workaround is to simulate groups: turn off real groups and insert your own owner-drawn “header rows” before each group of items. Then you can paint any colors you like:

' Setup once
ListView1.View = View.Details
ListView1.OwnerDraw = True
ListView1.ShowGroups = False

' Add a colored header row
Sub AddGroupHeader(text As String)
    Dim li = New ListViewItem(text) With {.Tag = "grp"}
    ListView1.Items.Add(li)
End Sub

Private Sub ListView1_DrawItem(sender As Object, e As DrawListViewItemEventArgs) Handles ListView1.DrawItem
    If CStr(e.Item.Tag) = "grp" Then
        e.Graphics.FillRectangle(Brushes.Navy, e.Bounds)
        TextRenderer.DrawText(e.Graphics, e.Item.Text, e.Item.Font, e.Bounds, Color.White, TextFormatFlags.VerticalCenter Or TextFormatFlags.Left)
    Else
        e.DrawDefault = True
    End If
End Sub

Private Sub ListView1_DrawSubItem(sender As Object, e As DrawListViewSubItemEventArgs) Handles ListView1.DrawSubItem
    If CStr(e.Item.Tag) <> "grp" Then e.DrawDefault = True
End Sub

Private Sub ListView1_DrawColumnHeader(sender As Object, e As DrawListViewColumnHeaderEventArgs) Handles ListView1.DrawColumnHeader
    e.DrawDefault = True
End Sub

Notes:

  • Keep these faux headers non-selectable in your UI logic if needed.
  • If you must keep real Groups, there is no supported way to recolor their header text. The OS draws them. LVGROUPMETRICS.

Recommended Answers

All 3 Replies

sweet man ... :cool:

so basically i can dig the listview as deep as i can afterall...

Hey would you mind answering this question as well ( if you have time)

I assume you already know how to do this or atleast read teh article...

If i set the ownerdraw property to true and then handle the drawitem event and also the drawheader event then do i have to take care of all the things ?
for example if in a certain case i want to change header portion only and leave the drawitem event to be handled by system is that possible ? or i just have to do all the things including drawing the items too ? cause i dont handle the drawitem event it wouldnt draw any items at all ...

To make owner-drawn HEADERS only, use:

Private Sub lv1_DrawItem(ByVal sender As Object, ByVal e As DrawListViewItemEventArgs) Handles lv1.DrawItem
		e.DrawDefault = True

	End Sub

	Private Sub lv1_DrawSubItem(ByVal sender As Object, _
	ByVal e As DrawListViewSubItemEventArgs) Handles lv1.DrawSubItem
		e.DrawDefault = True
	End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.