如何在ObjectListView中对项目进行排序?

4
我正在使用ObjectListView来显示项目列表。然而,当我点击列标题时,列不会被排序。
请参考下面粘贴的代码:
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;
}
3个回答

10

终于找到答案了。当我把 ObjectListViewShowGroups 属性改为 false 时,排序起作用了。默认情况下这个属性是 true!


1
谢谢!为什么这不是默认选项,我不知道! - Matthew Lock

4

我在ObjectListView(版本2.6.0)中进行了一些代码更改,以便在设置ShowGroupsTrue时,即使是非可分组列也能进行排序。

这样,对于将Groupable属性设置为False的列,可以进行普通排序,并且仍然可以通过按具有Groupable属性设置为True的列进行排序来对项目进行分组。

执行以下更改以获得此行为。

  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);
            }
        }
    }
    
现在可以将常规列和可分组列一起在一个 ObjectListView 中排序。

0
只是补充一下Igespee的回答,如果您在没有分组的列上使用搜索并且崩溃,您还必须更改以下两行。请记住,可能需要进行更多类似的更改(例如在之前的GetLastItemInDisplayOrder函数中),但这些更改至少可以防止每次按键时崩溃。
@@ -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 @@ 命名空间 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;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接