Search code examples
c#devexpressxtragridicomparable

IComparable required to sort column


One of the columns in my DevExpress xtragrid is not sorting, grouping or filtering. Answers to similar questions suggest I need to implement IComparable, but when I did that it no longer displays in the column at all.

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

What have I done wrong?

UPDATE:

Asked on DevExpress...


Solution

  • The disappearing content was an unrelated issue - a red herring. The column allowed sorting once I had implemented IComparable rather than 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);
    }
    

    Sourced from MSDN documentation for IComparable.CompareTo Method