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

标签 c# devexpress xtragrid icomparable

我的 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);
}

源自 MSDN 文档 IComparable.CompareTo Method

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

相关文章:

c# - 使用 DeveXpress 在 C# 中自动设置垂直列标题的高度

c# - 当多个输入具有相同的 "Name"时,如何使用 WatiN 填充特定输入字段?

android - 当虚拟键盘出现时,三星 Android 上的 Html 输入失去焦点

c# - 有没有办法设置 gridview Xtragrid 的垂直滚动位置?

c# - GridControl 的 SelectedMode (DevExpress)

c# - 在 DevExpress PivotGridControl 中使用 DisplayNameAttribute

winforms - 当 gridview is allowedit = false 时,如何在 GridView 中启用对新插入行的编辑?

c# - 我可以在任务列表完成时生成 IAsyncEnumerable 值吗?

c# - 在运行时更改 EF Core 上下文的连接字符串

c# - 在 ASP.NET MVC 中无需等待即可启动异步任务的两种方法之间的区别