public static void SetSortIcon(this ListView listViewControl, int columnIndex, SortOrder order) { IntPtr columnHeader = SendMessage(listViewControl.Handle, LvmGetheader, IntPtr.Zero, IntPtr.Zero); for (int columnNumber = 0; columnNumber <= listViewControl.Columns.Count - 1; columnNumber++) { var columnPtr = new IntPtr(columnNumber); var item = new Hditem { mask = Hditem.Mask.Format }; if (SendMessage(columnHeader, HdmGetitem, columnPtr, ref item) == IntPtr.Zero) { throw new Win32Exception(); } if (order != SortOrder.None && columnNumber == columnIndex) { switch (order) { case SortOrder.Ascending: item.fmt &= ~Hditem.Format.SortDown; item.fmt |= Hditem.Format.SortUp; break; case SortOrder.Descending: item.fmt &= ~Hditem.Format.SortUp; item.fmt |= Hditem.Format.SortDown; break; } } else { item.fmt &= ~Hditem.Format.SortDown & ~Hditem.Format.SortUp; } if (SendMessage(columnHeader, HdmSetitem, columnPtr, ref item) == IntPtr.Zero) { throw new Win32Exception(); } } }
public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 msg, IntPtr wParam, ref Hditem lParam);