11 Ocak 2013 Cuma

RadComboBox: custom sorting of items

The link (http://www.telerik.com/help/aspnet-ajax/combobox-how-to-implement-custom-sorting.html) explains how to implement a custom sorting mechanism for RadComboBox..
It says that Telerik RadComboBox sorts by using the text by default, but it seems to use the value instead..
In order to sort by text, the following class can be used:

1:  public class RadComboSortByText : IComparer  
2:    {  
3:      public int Compare(object x, object y)  
4:      {  
5:        RadComboBoxItem p1 = new RadComboBoxItem();  
6:        RadComboBoxItem p2 = new RadComboBoxItem();  
7:        if (x is RadComboBoxItem)  
8:          p1 = x as RadComboBoxItem;  
9:        if (y is RadComboBoxItem)  
10:          p2 = y as RadComboBoxItem;  
11:        int cmp = 0;  
12:        if (p1.ComboBoxParent.Sort == RadComboBoxSort.Ascending)  
13:        {  
14:          //here we compare the Text of the items  
15:          cmp = String.Compare(p1.Text, p2.Text, !p1.ComboBoxParent.SortCaseSensitive);  
16:        }  
17:        if (p1.ComboBoxParent.Sort == RadComboBoxSort.Descending)  
18:        {  
19:          //here we compare the Text of the items  
20:          cmp = String.Compare(p1.Text, p2.Text, !p1.ComboBoxParent.SortCaseSensitive) * -1;  
21:        }  
22:        return cmp;  
23:      }  
24:    }  
.. and itemBound of RadComboBox:
1:  protected void ComboEgitimDali_ItemDataBound(object sender, RadComboBoxItemEventArgs e)  
2:      {  
3:        ComboEgitimDali.Sort = RadComboBoxSort.Ascending;  
4:        ComboEgitimDali.SortItems(new RadComboSortByText());  
5:      }  
 <telerik:RadComboBox ID="ComboEgitimDali" runat="server" OnItemDataBound="ComboEgitimDali_ItemDataBound">  

Hiç yorum yok:

Yorum Gönder