开发者

How to sort items in ObjectListView?

开发者 https://www.devze.com 2023-04-11 10:59 出处:网络
I am using ObjectListView to display a list of items. However columns are not getting sorted when I click on the column headers.

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开发者_StackOverflow = 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;
}


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


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.


Just adding to Igespee's answer, if you use Search on a column that doesn't have groups and are crashing you also have to change the following two lines. Keep in mind more changes like this are probably necessary (like the GetLastItemInDisplayOrder function right before), but these will at least prevent it from crashing everytime a key is pressed.

@@ -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

精彩评论

暂无评论...
验证码 换一张
取 消