How do I set the column which has the header sort glyph, and its direction, in a .NET 2.0 WinForms ListView?
The listview is .net is not a managed control, it is a very thin wrapper around the Win32 ListView common control. It's not even a very good wrapper - it doesn't expose all the features of the real listview.
The Win32 listview common control supports drawing itself with themes. One of the themed elements is the header sort arrow. Windows Explorer's listview common control knows how to draw one of its columns with that theme element.
In case someone needs a quick solution (it draws up/down arrow at the beginning of column header text):
ListViewExtensions.cs:
public static class ListViewExtensions
{
    public static void DrawSortArrow(this ListView listView, SortOrder sortOrder, int colIndex)
    {
        string upArrow = "▲   ";
        string downArrow = "▼   ";
        foreach (ColumnHeader ch in listView.Columns)
        {
            if (ch.Text.Contains(upArrow))
                ch.Text = ch.Text.Replace(upArrow, string.Empty);
            else if (ch.Text.Contains(downArrow))
                ch.Text = ch.Text.Replace(downArrow, string.Empty);
        }
        if (sortOrder == SortOrder.Ascending)
            listView.Columns[colIndex].Text = listView.Columns[colIndex].Text.Insert(0, downArrow);
        else
            listView.Columns[colIndex].Text = listView.Columns[colIndex].Text.Insert(0, upArrow);
    }
}
Usage:
private void lstOffers_ColumnClick(object sender, ColumnClickEventArgs e)
{
    lstOffers.DrawSortArrow(SortOrder.Descending, e.Column);
}
I use unicode arrow characters in the title of the column and make the header a linkbutton.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With