英文:
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["Time"].Value).CompareTo(row2.Cells["Time"].Value));
}
<--> if needed
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论