2

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...

Michael Sandler
  • 1,290
  • 3
  • 17
  • 30
  • To sort I believe you would need IComparer and not IComparable. Check SO answer for details :http://stackoverflow.com/questions/538096/when-to-use-icomparablet-vs-icomparert. IComparable would just tell you equals or not but an implementation of IComparer would tell you greater than, equals and less than, thereby allowing you to sort. – Ravi Y Jan 25 '13 at 07:41
  • @ryadavilli: It should be `IComparable` as it in an intrinsic comparison. – Dave New Jan 25 '13 at 07:44
  • @davenewza I am not sure about the datagrid, but in general to sort you would need to know greater or less or equals. A simple CompareTo which just tells equals or not equals, would not help to sort the data. Can you guide me to some documentation to help me understand this? – Ravi Y Jan 25 '13 at 07:53
  • It's best to post this question on the DevExpress forums. `XtraGrid` is such a terribly designed control. There are probably various properties that you need to set too. – Dave New Jan 25 '13 at 07:54
  • @ryadavilli: [IComparable on MSDN](http://msdn.microsoft.com/en-us/library/4d7sx9hd.aspx). `IComparable` definitely does indicate greater than, equals or less than. It is the defacto interface used for object comparison. – Dave New Jan 25 '13 at 07:57
  • @davenewza Ahhh yes it Does. Thanks for pointing it to me. – Ravi Y Jan 25 '13 at 07:59

2 Answers2

2

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

Michael Sandler
  • 1,290
  • 3
  • 17
  • 30
0

It looks like your CompareTo Method is wrong. Try adding the following to the CompareTo() Method and see if it's working:

    public int CompareTo(Flow other)
    {
      // Alphabetic sort if name is equal.
      if this.Name == other.Name
      {
        return this.Name.CompareTo(other.Name);
      }
     //Default sort.
     return other.Name.CompareTo(this.Name);
    }

Let me know if it sorted out your problem.

  • Thanks Johan, but this does not change the behaviour of the column. – Michael Sandler Jan 25 '13 at 09:16
  • Pleasure Michael, I just want to find out, are you adding your values to a List maybe? If not maybe try to create a List of Flow objects eg: List list = new List(); and after adding all the objects to the list try to use list.Sort(); to try and sort your list and then try to populate your grid with the data –  Jan 25 '13 at 09:48
  • I was trying to use the built-in devexpress xtragrid functionality. It's working now; see my answer. – Michael Sandler Jan 25 '13 at 10:36