How to sort items in an ObjectListView?

I am using an ObjectListView to display a list of items. However, the columns are not sorted when I click on the column headers.

Please check out the code below:

 public class Stock { public Stock() { } public Stock(string name, double lastPrice, double greenTrend, double redTrend, double lastGreenValue, double lastRedValue) { this.Name = name; this.LastPrice = lastPrice; this.GreenTrend = greenTrend; this.RedTrend = redTrend; this.LastGreenValue = lastGreenValue; this.LastRedValue = lastRedValue; } public string Name { get { return name; } set { name = value; } } private string name; public double LastPrice { get { return lastPrice; } set { lastPrice = value; } } private double lastPrice; public double GreenTrend { get { return greenTrend; } set { greenTrend = value; } } private double greenTrend; public double RedTrend { get { return redTrend; } set { redTrend = value; } } private double redTrend; public double LastGreenValue { get { return lastGreenValue; } set { lastGreenValue = value; } } private double lastGreenValue; public double LastRedValue { get { return lastRedValue; } set { lastRedValue = value; } } private double lastRedValue; } 
+4
source share
3 answers

Finally found the answer. When I changed the ShowGroups property of ShowGroups to false, the sorting worked. The default value is true!

+7
source

I made a few code changes in ObjectListView (version 2.6.0) to enable sorting by non-group columns, even if ShowGoups set to True .

Thus, you can have normal sorting for columns with the Groupable property set to False , and still be able to group items when sorting by a column with the Groupable property set to True.

Make the following changes to get this behavior.

  • In the PostProcessRows() method, replace this code (around line 7729 ):

     if (this.ShowGroups) { foreach (ListViewGroup group in this.Groups) { foreach (OLVListItem olvi in group.Items) { ... 

    :

     if (this.ShowGroups && ((this.LastSortColumn != null) && this.LastSortColumn.Groupable)) { foreach (ListViewGroup group in this.Groups) { foreach (OLVListItem olvi in group.Items) { ... 
  • In the DoSort() method, replace this code (around 7391 ):

     if (!args.Handled) { // Sanity checks if (args.ColumnToSort != null && args.SortOrder != SortOrder.None) { if (this.ShowGroups) this.BuildGroups(args.ColumnToGroupBy, args.GroupByOrder, args.ColumnToSort, args.SortOrder, args.SecondaryColumnToSort, args.SecondarySortOrder); else if (this.CustomSorter != null) this.CustomSorter(args.ColumnToSort, args.SortOrder); else this.ListViewItemSorter = new ColumnComparer(args.ColumnToSort, args.SortOrder, args.SecondaryColumnToSort, args.SecondarySortOrder); } } 

    :

     if (!args.Handled) { // Sanity checks if (args.ColumnToSort != null && args.SortOrder != SortOrder.None) { if (this.ShowGroups && args.ColumnToGroupBy.Groupable) this.BuildGroups(args.ColumnToGroupBy, args.GroupByOrder, args.ColumnToSort, args.SortOrder, args.SecondaryColumnToSort, args.SecondarySortOrder); else if (this.CustomSorter != null) this.CustomSorter(args.ColumnToSort, args.SortOrder); else { this.Groups.Clear(); this.ListViewItemSorter = new ColumnComparer(args.ColumnToSort, args.SortOrder, args.SecondaryColumnToSort, args.SecondarySortOrder); } } } 

Now you can sort regular and grouped columns in just one ObjectListView in combination.

+4
source

Just adding Igespee to the answer, if you use "Search" in a column that has no groups and fails, you also need to change the following two lines. Keep in mind that additional changes are probably necessary (for example, the GetLastItemInDisplayOrder function), but this will at least prevent it from crashing each time you press a key.

@@ - 3948.7 +3948.7 @@ namespace BrightIdeasSoftware

  /// <param name="n"></param> /// <returns></returns> public virtual OLVListItem GetNthItemInDisplayOrder(int n) { -- if (!this.ShowGroups) ++ if (!this.ShowGroups || this.Groups.Count==0) return this.GetItem(n); foreach (ListViewGroup group in this.Groups) { 

@@ - 3969.7 +3969.7 @@ namespace BrightIdeasSoftware

  /// <param name="itemIndex"></param> /// <returns></returns> public virtual int GetDisplayOrderOfItemIndex(int itemIndex) { -- if (!this.ShowGroups) ++ if (!this.ShowGroups || this.Groups.Count == 0) return itemIndex; 
0
source

All Articles