This is a migrated thread and some comments may be shown as answers.

Sort a column containing a class

1 Answer 82 Views
GridView
This is a migrated thread and some comments may be shown as answers.
hermann
Top achievements
Rank 1
hermann asked on 08 Jul 2011, 03:46 PM
Hi,
my ItemSource contains an ObservableCollection of Players, (taken from the GridViewComboBoxColumn-ChangeDataContext.zip)
where Player has an extra property, a class named CombinedID. It is defined as
public partial class CombinedID
          : INotifyPropertyChanged
          , IEquatable<CombinedId>
          , IComparable<CombinedId>
          , IEqualityComparer<CombinedId>
{
    private int DbIdField;
    private Guid GuidField;
 
    public CombinedID(int i, Guid guid){
        DbId = i;
        Guid = guid;
    }
 
    public int DbId {
        get {   return this.DbIdField; }
        set {
            if ((this.DbIdField.Equals(value) != true)) {
                this.DbIdField = value;
                this.RaisePropertyChanged("DbId");
            }
        }
    }
 
    public System.Guid Guid {
        get { return this.GuidField; }
        set {
            if ((this.GuidField.Equals(value) != true)) {
                this.GuidField = value;
                this.RaisePropertyChanged("Guid");
            }
        }
    }
 
    public event PropertyChangedEventHandler PropertyChanged;
    protected void RaisePropertyChanged(string propertyName) {
        PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
        if ((propertyChanged != null)) {
            propertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
 
    public override string ToString() {
        return DbId.ToString();
    }
 
    public bool Equals(CombinedID other) { // for IEquitable<T>
        if (other == null) return false;
        return this.DbId == other.DbId;
    }
    public bool Equals(CombinedID a, CombinedID b) { // for IEqualityComparer<T>
        if(a != null && b != null)
            return a.DbId == b.DbId;
        if (a == null && b == null) return true;
        return false;
    }
     
    public int CompareTo(CombinedID other) { // for IComparable<T>
        if (other == null) return 1;
        return this.DbId.CompareTo(other.DbId);
    }
 
    public override int GetHashCode()   { // for IEquitable<T>
        return DbId.GetHashCode() ^ Guid.GetHashCode();
    }
 
    public int GetHashCode(CombinedID a)     { // for IEqualityComparer<T>
        return a.GetHashCode();
    }
}

So, Player has the following properties (excerpt):
  • public int SortID
  • public string Name
  • public CombinedID ID

Unfortunately, despite all the implemented interfaces, the DataGrid column containing this CombinedId refuses sorting. It just does nothing.
The XAML code for the extra column is
<telerik:GridViewDataColumn DataMemberBinding="{Binding ID}"/>
and I have tried SortMemberPath="DbId" and SortMemberPath="ID.DbId"
but nothing seems to work.
Can you please give me a hint why this column cannot be sorted?

1 Answer, 1 is accepted

Sort by
0
hermann
Top achievements
Rank 1
answered on 08 Jul 2011, 03:58 PM
I found out why it didn't work:
It was a typo in the SortMemberPath assignment (SortMemberPath="ID.DbId"), so the sortmember was never set - and Visual Studio remained politely silent.
Sorry, for bothering you.

btw, you don't need the IEqualityComparer<T>
Tags
GridView
Asked by
hermann
Top achievements
Rank 1
Answers by
hermann
Top achievements
Rank 1
Share this question
or