0

I have a listview (lvMap) with 3 columns (Map, From, To) I am trying to write a method that is called as soon as my form loads. this method should look at the listview items and and sort them only by 2 columns "Map" and "From" in ascending order, i dont want it to sort the "To" column. I have written the code below but it sorts every single column, is there a way to leave a column out of the sorting procedure. Thanks.

private void sortListViewOrder()
{
    lvMappings.Sorting = SortOrder.Ascending;
    lvMappings.Sort();
}
Omar
  • 16,329
  • 10
  • 48
  • 66
floormind
  • 1,868
  • 5
  • 31
  • 85

3 Answers3

0

I would suggest you consult the following MSDN article, hopeful it answers your question:

http://support.microsoft.com/kb/319401

Basically you need to create a ListViewColumnSorter instance and add it to your ListView control.

From there on the article will have enough information :)

0

You have to do it using ListViewColumnSorter . Following KB Link has the sample code to do that.

http://support.microsoft.com/kb/319401

You can assign the column to be sorted using,

 Create an instance of a ListView column sorter and assign it 
// to the ListView control.
lvwColumnSorter = new ListViewColumnSorter();
this.listView1.ListViewItemSorter = lvwColumnSorter;

lvwColumnSorter.SortColumn = Column;
Rajesh Subramanian
  • 6,400
  • 5
  • 29
  • 42
  • Hey, thanks but this article is based on if the column has been clicked. I dont want to have to click the column to sort its item. – floormind May 14 '13 at 13:02
0

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!

Community
  • 1
  • 1
PrfyVdlx
  • 21
  • 2
  • If you are wondering what `ListViewColumnSorter` is, it is found here at the bottom of the page: http://support.microsoft.com/kb/319401 – Andy Oct 22 '18 at 14:25