gpt4 book ai didi

c# - IComparable 需要对列进行排序

转载 作者:太空宇宙 更新时间:2023-11-03 11:09:43 24 4
gpt4 key购买 nike

我的 DevExpress xtragrid 中的一列没有排序、分组或过滤。类似问题的答案表明我需要实现 IComparable,但当我这样做时,它根本不再显示在列中。

public class Flow : System.IComparable<Flow>
{
public Flow(int id, string name, string description)
{
this.ID = id;
this.Name = name;
this.Description = description;
}

public int ID { get; private set; }

public string Name { get; private set; }

public string Description { get; private set; }

public override string ToString()
{
return Name;
}

public override bool Equals(object obj)
{
Flow flow = obj as Flow;
if (flow == null) return false;
return this.ID == flow.ID;
}

public static bool operator ==(Flow flow1, Flow flow2)
{
if (object.ReferenceEquals(null, flow1))
return object.ReferenceEquals(null, flow2);
return flow1.Equals(flow2);
}

public static bool operator !=(Flow flow1, Flow flow2)
{
return !(flow1 == flow2);
}

public override int GetHashCode()
{
return ID;
}

public int CompareTo(Flow other)
{
return this.Name.CompareTo(other.Name);
}
}

我做错了什么?

更新:

DevExpress 上提问...

最佳答案

消失的内容是一个不相关的问题 - 转移注意力。一旦我实现了 IComparable,该列就允许排序而不是 IComparable<Flow>

public int CompareTo(object obj)
{
if (object.ReferenceEquals(null, obj))
return 1;
Flow flow = obj as Flow;
if (flow == null)
throw new ArgumentException("Object is not of type Flow");
return this.Name.CompareTo(flow.Name);
}

源自 IComparable.CompareTo Method 的 MSDN 文档

关于c# - IComparable 需要对列进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14517409/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com