Search code examples
c#sortingobjectlistview

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;
}

Solution

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