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

Sorting an Image column based on the value of another property

1 Answer 121 Views
GridView
This is a migrated thread and some comments may be shown as answers.
johns
Top achievements
Rank 1
johns asked on 16 Aug 2007, 05:01 AM
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.

1 Answer, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 17 Aug 2007, 01:20 PM
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(); 
 
table.Columns.Add("First"typeof(int)); 
table.Columns.Add("Second"typeof(string)); 
table.Columns.Add("Third"typeof(MyData)); 
 
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) 
    { 
        this.data = 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; 
    } 
    #endregion 


Sincerely yours,
Jack
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
GridView
Asked by
johns
Top achievements
Rank 1
Answers by
Jack
Telerik team
Share this question
or