Assign Images and Sorting logic to a Windows Forms ListView Control

1. Use Visual C# .NET to start a new Windows application.

2. In Form1, add a ListView control, a Button control, and an ImageList control. The default names for these objects are listView1, button1, and imageList1, respectively.

3. In the imageList1 Properties dialog box, click the ellipsis (…) next to the Images property to open the Images Collection Editor dialog box, and then add two images to the control.

4. In the code window of Form1.cs, add the following statement after the other using statements:

using System.Runtime.InteropServices;

5. Copy and paste the following code in the Form1 window after the Private statements:

Code..

public const UInt32 LVM_GETHEADER = 4127;public const UInt32 HDM_SETIMAGELIST = 4616;public const UInt32 LVM_SETCOLUMN = 4122;public const uint LVCF_FMT = 1;

public const uint LVCF_IMAGE = 16;

public const int LVCFMT_IMAGE = 2048;

// Define the LVCOLUMN for use with interop [StructLayout(LayoutKind.Sequential, Pack=8, CharSet=CharSet.Auto)]

public struct LVCOLUMN { public uint mask; public int fmt; public int cx; public IntPtr pszText; public int cchTextMax; public int iSubItem; public int iImage; public int iOrder; }

// Declare two overloaded SendMessage functions. The

// difference is in the last parameter. [DllImport(“user32.dll”)] public static extern IntPtr SendMessage( IntPtr hWnd, UInt32 Msg, UInt32 wParam, UInt32 lParam);

[DllImport(“User32”, CharSet=CharSet.Auto)] public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 msg, UInt32 wParam, ref LVCOLUMN lParam);

6. In the Form1_Load event, add the following code:listView1.View = View.Details;listView1.Columns.Add(“Column 0”, 60, HorizontalAlignment.Left);listView1.Columns.Add(“Column 1”, 60, HorizontalAlignment.Left);

7. Copy and paste the following method and invoke it from ListView_Columnclick event.

Do Sorting…

public void DoSorting(RTLListView lst, int ColNum) { IntPtr hwnd; IntPtr lret;int i;int iIndex;//–set the sorting order in list view {if (lst.Sorting == SortOrder.Ascending){lst.Sorting = SortOrder.Descending;

iIndex = 4;

}

else

{

lst.Sorting = SortOrder.Ascending;

iIndex = 3;

}

lst.ListViewItemSorter = new ColumnSorter(lst.Sorting, ColNum);

lst.Sort();

}

//–Assign the ImageList to the header control.

//–The header control includes all columns.

//–Get a handle to the header control.

hwnd = SendMessage(lst.Handle, LVM_GETHEADER,(UInt32) 0, (UInt32)0);

//–Add the ImageList to the header control.

lret = SendMessage(hwnd, HDM_SETIMAGELIST,(UInt32) 0,(UInt32) ImgLstSorting.Handle);

//–The code to follow uses successive images in the ImageList to loop

//through all columns and place successive columns in the ColumnHeader.

//–This code uses LVCOLUMN to define alignment. By using LVCOLUMN here,

//–you reset the alignment if it was defined in the designer.

//–If you need to set the alignment, you must change the code below to set it here.

for (i = 0; i <= lst.Columns.Count – 1; i++)

{

//–Use the LVM_SETCOLUMN message to set the column’s image index.

LVCOLUMN col;

col.mask = LVCF_FMT |LVCF_IMAGE;

col.fmt = LVCFMT_IMAGE;

//–The image to use from the Image List.

if (i == ColNum)

{

col.iImage = iIndex;

}

else

{

col.iImage = -1;

}

col.cchTextMax = 0;

col.cx = 0;

col.iOrder = 0;

col.iSubItem = 0;

col.pszText = (IntPtr)0;

//–Send the LVM_SETCOLUMN message.

//–The column to which you are assigning the image is defined in the third parameter.

lret = SendMessage(lst.Handle,LVM_SETCOLUMN,(UInt32) i,ref col);

}

}

ColumnSorter Class…

public class ColumnSorter : IComparer{

#region “– Private –“

#region “– Variables/Properties –“

private SortOrder m_SortOrder;

private int m_CurrentColumn;

#endregion

#region “– Methods –“

#endregion

#endregion

#region “– internal –“

#region “– Variables/Properties –“

#endregion

#region “– Methods –“

#endregion

#endregion

internal ColumnSorter(SortOrder p_SortOrder, int p_Column)

{

m_SortOrder = p_SortOrder;

m_CurrentColumn = p_Column;

}

int IComparer.Compare(object p_Actual, object p_Desried)

{

int RetVal = 0;

try

{

if (m_SortOrder == SortOrder.Ascending)

{

if (p_Actual is ListViewItem)

{

RetVal = string.Compare(((ListViewItem)p_Actual).SubItems[m_CurrentColumn].Text, ((ListViewItem)p_Desried).SubItems[m_CurrentColumn].Text);

}

else

{

}

}

else

{

if (p_Actual is ListViewItem)

{

RetVal = string.Compare(((ListViewItem)p_Desried).SubItems[m_CurrentColumn].Text, ((ListViewItem)p_Actual).SubItems[m_CurrentColumn].Text);

}

else

{

}

}

}

catch (Exception )

{

}

return RetVal;

}

}

Continue ReadingAssign Images and Sorting logic to a Windows Forms ListView Control