views:

271

answers:

2

How can you change the background color of the Headers of a ListView?

A: 

You can do this by setting the OwnerDraw property for the list view to true.

This then allows you to provide event handlers for the listview's draw events.

There is a detailed example on MSDN

Below is some example code to set the header colour to red:

    private void listView1_DrawColumnHeader(object sender,
                                            DrawListViewColumnHeaderEventArgs e)
    {
        e.Graphics.FillRectangle(Brushes.Red, e.Bounds);
        e.DrawText();
    }

I think (but am happy to be proved wrong) that with OwnerDraw set to true, you will need to also provide handlers for the other draw events that have default implementations as shown below:

    private void listView1_DrawItem(object sender,
                                    DrawListViewItemEventArgs e)
    {
        e.DrawDefault = true;
    }

I certainly haven't managed to make the listview draw the items without that.

David Hall