public static void SetSortIcon(this ListView listView, int columnIndex, SortOrder order) { IntPtr columnHeader = SendMessage(listView.Handle, LVM_GETHEADER, IntPtr.Zero, IntPtr.Zero); for (int columnNumber = 0; columnNumber < listView.Columns.Count; columnNumber++) { IntPtr columnPtr = new IntPtr(columnNumber); LVCOLUMN lvColumn = new LVCOLUMN(); lvColumn.mask = HDI_FORMAT; SendMessageLVCOLUMN(columnHeader, HDM_GETITEM, columnPtr, ref lvColumn); if (order != SortOrder.None && columnNumber == columnIndex) { switch (order) { case System.Windows.Forms.SortOrder.Ascending: lvColumn.fmt &= ~HDF_SORTDOWN; lvColumn.fmt |= HDF_SORTUP; break; case System.Windows.Forms.SortOrder.Descending: lvColumn.fmt &= ~HDF_SORTUP; lvColumn.fmt |= HDF_SORTDOWN; break; } lvColumn.fmt |= (HDF_LEFT | HDF_BITMAP_ON_RIGHT); } else { lvColumn.fmt &= ~HDF_SORTDOWN & ~HDF_SORTUP & ~HDF_BITMAP_ON_RIGHT; } SendMessageLVCOLUMN(columnHeader, HDM_SETITEM, columnPtr, ref lvColumn); } }
private static extern IntPtr SendMessageLVCOLUMN(IntPtr hWnd, int Msg, IntPtr wParam, ref LVCOLUMN lPLVCOLUMN);