I needed this feature, or function, in the ListView control. The suggestion to use an Extension Class I first saw here. I tried it and it worked, but only now I can tell how to easily do it. Refer to this reference question:
How to prevent flickering in ListView when updating a single ListViewItem's text?
Step 1: Create a (separate) ControlExtensions class in your project, and paste this code:
using System.Reflection;
using System.Windows.Forms;
namespace [YourNameSpace]
{
public static class ControlExtensions
{
public static void DoubleBuffering(this Control control, bool enable)
{
var method = typeof(Control).GetMethod("SetStyle", BindingFlags.Instance | BindingFlags.NonPublic);
method.Invoke(control, new object[] { ControlStyles.OptimizedDoubleBuffer, enable });
}
}
}
Step 2: Define the following in the WinForms that has the ListView:
private ListViewColumnSorter lvwColumnSorter = null;
After InitializeComponent(); section, define the following:
lvwColumnSorter = new ListViewColumnSorter();
this.lvwRunningProcesses.ListViewItemSorter = lvwColumnSorter;
lvwColumnSorter._SortModifier = ListViewColumnSorter.SortModifiers.SortByText;
Step 3: In the Form Load event, add these lines after the List View is populated:
// Sort in ascending order Column 0
lvwColumnSorter.SortColumn = 0;
lvwColumnSorter.Order = SortOrder.Ascending;
this.lvwRunningProcesses.Sort();
That's it!