Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort items in ObjectListView?

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

Please refer the code pasted 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;
}
like image 462
Martin Avatar asked Dec 10 '22 05:12

Martin


2 Answers

Finally found the answer. When I changed the ShowGroups property of ObjectListView to false, sorting worked. This was set to true by default!

like image 74
Martin Avatar answered Jan 02 '23 21:01

Martin


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

This way it's possible to have a normal sorting for columns with the Groupable property set to False and still be able to group the items when sorting by a column which has the Groupable property set to True.

Make the following changes to get this behavior.

  1. In the PostProcessRows() method, exchange this code (around line 7729):

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

    by this:

    if (this.ShowGroups && ((this.LastSortColumn != null) && this.LastSortColumn.Groupable)) {
        foreach (ListViewGroup group in this.Groups) {
            foreach (OLVListItem olvi in group.Items) {
                ...
    
  2. In the DoSort() method, exchange 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);
        }
    }
    

    by this:

    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 it'll be possible to sort regular and groupable columns all in one ObjectListView combined.

like image 31
lgespee Avatar answered Jan 02 '23 21:01

lgespee