Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView DrawColumnHeader is not working

I have the following method to set background color and Font for the list view's column headers but it is not working. Anybody can help me?

private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
    using (StringFormat sf = new StringFormat())
    {
        // Store the column text alignment, letting it default
        // to Left if it has not been set to Center or Right.
        switch (e.Header.TextAlign)
        {
            case HorizontalAlignment.Center:
                sf.Alignment = StringAlignment.Center;
                break;
            case HorizontalAlignment.Right:
                sf.Alignment = StringAlignment.Far;
                break;
        }

        // Draw the standard header background.
        e.DrawBackground();

        // Draw the header text.
        using (Font headerFont = new Font("Helvetica", 25, FontStyle.Bold)) //Font size!!!!
        {
            e.Graphics.DrawString(e.Header.Text, headerFont, Brushes.Black, e.Bounds, sf);
        }
    }
    return;
}

It doesn't matter I use this method or not. It remains same. Nothing changes. Is it because of properties of the list view?

like image 312
a-f-a Avatar asked Oct 27 '25 10:10

a-f-a


1 Answers

Your code looks ok. If it doesn't do anything you have forgotten to set the OwnerDraw property to true.

This also means that you need to add code for the other two draw events. The default actions will do:

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


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

Don't forget to hook up the events ;-)

like image 158
TaW Avatar answered Oct 30 '25 01:10

TaW



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!