DataGridView按一个主列排序,但是按另一列中特定值将其分组。

huangapple go评论106阅读模式
英文:

DataGridView sort by one main column but order so that those with a specific value in another column are grouped together

问题

我正在尝试实现一个DataGridView来显示一些信息。

我有两列,'Time'(这是执行当前操作所花费的时间)和'Status'(显示用户当前状态)。

有3种状态,'Ready'、'Incall'和'Pause'。

我希望所有状态为'Incall'的条目都显示在DataGridView的底部,'Ready'在这3个状态的中间,'Pause'在顶部。同时仍然按照'Time'列中的最高值进行排序。

一个示例如下:

Status Time

PAUSE        104
PAUSE        99
PAUSE        12
READY        50
READY        12
READY        3
INCALL       900
INCALL       200
INCALL       3
INCALL       1

我已经查看了Microsoft文档中关于实现IComparer的部分这里

它按照我的要求按'Status'进行排序,但是某些值按照'Time'排序时顺序不正确,例如:

PAUSE      50
PAUSE      43
READY      100
READY      50
READY      106
READY      190
INCALL     900
INCALL     760
INCALL     400
INCALL     765

以下是我实现CustomComparer并实现IComparer的代码:

private class CustomComparer : IComparer
{
    private static int sortModifier = -1;

    public CustomComparer(SortOrder sortOrder)
    {
        if(sortOrder == SortOrder.Descending)
        {
            sortModifier = -1;
        } 
        else if (sortOrder == SortOrder.Ascending)
        {
            sortModifier = 1;
        }                    
    }

    public int Compare(object x, object y)
    {
        DataGridViewRow row1 = (DataGridViewRow)x;
        DataGridViewRow row2 = (DataGridViewRow)y;

        int result = row1.Cells["Status"].Value.ToString().CompareTo(row2.Cells["Status"].Value.ToString());

        // 如果列相同,则根据Time排序
        if (result == 0)
        {
            result = System.String.Compare(row1.Cells["Time"].Value.ToString(), row2.Cells["Time"].Value.ToString());
        }
                
        return result * sortModifier;
    }
}

在填充DataGridView后,我调用它的地方如下:

monitor_DataGridView.Sort(new CustomComparer(SortOrder.Descending));

我认为问题可能在于CustomComparer首先比较了第一列,如果它们相等,就不会比较后面的'time'列,这解释了为什么大多数情况下是有序的(可能是巧合)。

任何帮助将不胜感激。
最好,

英文:

I'm trying to implement a DataGridView to display some information.

I have two columns 'Time' (this is the time spent doing the current action) and 'Status' (which shows the current status of the user)

There are 3 statuses, 'Ready' 'Incall' and 'Pause'

I want all those with status 'Incall' to be at the bottom of the DataGridView, 'Ready' in the middle of the 3 and 'Pause' at the top. While still sorting by the highest value in the 'Time' column.

An example would be

Status Time

PAUSE        104
PAUSE        99
PAUSE        12
READY        50
READY        12
READY        3
INCALL       900
INCALL       200
INCALL       3
INCALL       1

I've had a look at implementing IComparer using Microsofts documentation here

And it does what I want in terms of ordering by the Status, however some of the values are in the wrong order by 'Time' so I get for example:

PAUSE      50
PAUSE      43
READY      100
READY      50
READY      106
READY      190
INCALL     900
INCALL     760
INCALL     400
INCALL     765

Here's where I implement the CustomComparer which implements IComparer


private class CustomComparer : IComparer
{
    private static int sortModifier = -1;

    public CustomComparer(SortOrder sortOrder)
    {
        if(sortOrder == SortOrder.Descending)
        {
            sortModifier = -1;
        } else if (sortOrder == SortOrder.Ascending)
        {
            sortModifier = 1;
        }                    
    }

    public int Compare(object x, object y)
    {
        DataGridViewRow row1 = (DataGridViewRow)x;
        DataGridViewRow row2 = (DataGridViewRow)y;

        
        result = row1.Cells["Status"].Value.ToString().CompareTo(row2.Cells["Status"].Value.ToString());

        // If the the columns are the same sort based on Time
        if (result == 0)
        {
            result = System.String.Compare(row1.Cells["Time"].Value.ToString(), row2.Cells["Time"].Value.ToString());
        }
                
        // If the the columns are the same sort based on Time
        return result * sortModifier;
    }
}

And here's where I call it after populating the GridListView

monitor_DataGridView.Sort(new CustomComparer(SortOrder.Descending));

I think it must be due to the fact that the CustomComparer compares the first column, and if they are equal does not then compare the 'time' column afterwards which would explain why most are in order (could be coincidence)

Any help would be appreciated.
Best,

答案1

得分: 1

如果需要,我猜你需要将时间作为整数进行比较而不是字符串:

if (result == 0) {
    result = ((int)row1.Cells["Time"].Value).CompareTo(row2.Cells["Time"].Value));
}

<--> 如果需要

英文:

I guess you need to compare Time as integer instead of string:

    if (result == 0) {
        result = ((int)row1.Cells[&quot;Time&quot;].Value).CompareTo(row2.Cells[&quot;Time&quot;].Value));
    }

<--> if needed

huangapple
  • 本文由 发表于 2023年6月15日 20:32:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76482519.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定