Sorting an Image column based on the value of another property

2 posts, 0 answers
  1. johns
    johns avatar
    20 posts
    Member since:
    Jul 2007

    Posted 16 Aug 2007 Link to this post

    I have a Grid set so that two fields are displayed.  The first is a Bitmap that is generated by a Count property of the object that is bound.
    The other is just some informational text.

    I do not want to display a separate column for the count property but when a user clicks on the header for the Image, I want it sorted based on the count. 

    Is this possible?  I thought I could perhaps overide the field in the sort expression by adding a SortChangingEventHandler, but when I add this event handler it is not getting called.

  2. Jack
    Jack avatar
    2335 posts

    Posted 17 Aug 2007 Link to this post

    Hi johns,

    In order to do custom sorting you must implement the IComparable interface in your custom class. Here is an example how to do this:

    DataTable table = new DataTable(); 
    Random r = new Random(); 
    for (int i = 0; i < 50; i++) 
        table.Rows.Add(i, i.ToString(),  new MyData(i.ToString(), r.Next(50))); 
    this.radGridView1.DataSource = table; 

    Here is the implementation of MyData class:

    public class MyData: IComparable 
        string data; 
        int weight; 
        public string Data 
            get { return data; } 
            set { data = value; } 
        public MyData(): this("", 0) 
        public MyData(string data, int weight) 
   = data; 
            this.weight = weight; 
        public override string ToString() 
            return data + " " + weight; 
        #region IComparable Members 
        public int CompareTo(object obj) 
            if (obj is MyData) 
                if (((MyData)obj).weight < weight) 
                    return -1; 
                else if (((MyData)obj).weight > weight) 
                    return 1; 
            return 0; 

    Sincerely yours,
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. DevCraft banner
Back to Top