Arrow on a ListView Column Header

D

Dan

Hi,

I would like my list view controls column headers to display a "down arrow /
triangle" to indicate by which column they are sorted.

I don't want the ListView to actually sort any data as the sorting is done
elsewhere, I just want the arrow!

Thanks in advance

Dan
 
W

Wiktor Zychla

I would like my list view controls column headers to display a "down arrow
/
triangle" to indicate by which column they are sorted.

below a code snippet taken from my source.
since I do not want to include much code here, you'll have to work out
missing Win32 declarations by yourself.

regards,
Wiktor Zychla

// ListView variable is global here
public void SetHeaderBitmap( int iColumn, Bitmap bBitmap )
{
// win32 structure
HDITEM h = new HDITEM();
h.mask = (int)HeaderItemFlags.HDI_BITMAP |
(int)HeaderItemFlags.HDI_FORMAT;
h.hbm = bBitmap.GetHbitmap();
h.fmt = (int)HeaderItemFormat.HDF_LEFT |
(int)HeaderItemFormat.HDF_STRING |
(int)HeaderItemFormat.HDF_BITMAP ;

// take a pointer to header control
IntPtr hHeader = (IntPtr)WindowsAPI.SendMessage( this.lstLista.Handle,
(int)ListViewMessages.LVM_GETHEADER, 0, 0 );
// set the header icon
WindowsAPI.SendMessage( hHeader, (int)HeaderControlMessages.HDM_SETITEM,
iColumn, ref h );
}
 
T

ToddT

The trick is to change the column text value when sorting by adding 3
spaces
and the Unicode characters for the small black Up and Down triangles.
Strip
them off when changing columns.

Sample code for lurkers:
Protected Overrides Sub dvDisplay_ColumnClick(ByVal sender As Object,
ByVal
e As System.Windows.Forms.ColumnClickEventArgs)

'remove sort icon from old column.

If SortColumn <> -1 Then

lv.Columns.Item(SortColumn).Text =
lv.Columns.Item(SortColumn).Text.TrimEnd(Chr(32), Chr(32), Chr(32),
ChrW(&H25B2), ChrW(&H25BC))

End If

If e.Column <> SortColumn Then

SortColumn = e.Column

mSortDirection = "ASC"

'add down icon

lv.Columns.Item(SortColumn).Text &= " " & ChrW(&H25B2)

Else

If mSortDirection = "ASC" Then

mSortDirection = "DESC"

'do the sort

'add down icon

lv.Columns.Item(SortColumn).Text &= " " & ChrW(&H25BC)

Else

mSortDirection = "ASC"

'do the sort

'add up icon

lv.Columns.Item(SortColumn).Text &= " " & ChrW(&H25B2)

End If

End If

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top