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

[Solved] Sorting on GridView doesn't work with this kind of databinding

1 Answer 81 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Gauri
Top achievements
Rank 1
Gauri asked on 02 Dec 2010, 12:26 AM

Hello,

I am trying to make Sorting work on clicking column headers on the RadGridView. I have very complex data in the form of List of dictionary entries and the grid is bound to the data with a converter in between to map the data correctly with the columns. The data being dynamic I have as many columns as there are keys in each dictionary entry. The columns are created in the code behind and using DataMemberBinding I am mapping the columns with the appropriate data.

My problem is that Sorting just doesn’t seem to be working in this scenario. I am not sure what I am missing in this.

Here is my source code:

 

 

public class ViewItem : INotifyPropertyChanged

 

 

 

 

{

 

 

 

 

    private Dictionary<string, object> _Fields;

 

 

 

 

    public Dictionary<string,object> Fields

 

 

     {

 

 

        get

 

 

 

 

 

        {

 

 

 

 

           if (_Fields == null)

 

 

                 _Fields =

 

new Dictionary<string, object>();

 

 

     

 

            return _Fields;

 

         }

 

 

 

        set

 

 

 

 

 

 

        {

 

 

            _Fields =

 

value;

 

 

 

 

 

            if(this.PropertyChanged!=null)

 

 

 

                this.PropertyChanged(this, new PropertyChangedEventArgs("Fields"));

 

        }

    }

 

 

 

    public event PropertyChangedEventHandler PropertyChanged;

 

 

 

    public ViewItem()

 

 

    {

    }

}

 

 


public
partial class MainPage : UserControl

 

 

 

 

{

 

 

 

    private ObservableCollection<ViewItem> _ViewItems;

 

 

 

    public ObservableCollection<ViewItem> ViewItems

 

    {

 

 

       get

 

 

 

 

        {

 

 

 

            return _ViewItems;

 

         }

 

 

       set

 

 

 

 

        {

 

            _ViewItems =

 

value;

 

         }

    }

 

 

   public ObservableCollection<TestClass> TestList { get; set; }

 

 

 

 

    public MainPage()

    {

        InitializeComponent();

        CreateDataSource();

        ConfigureGrid();

 

        TheGrid.SetBinding(

 

        RadGridView.ItemsSourceProperty, new Binding("ViewItems") { Mode =     BindingMode.TwoWay, Source = this });

 

    }

 

 

 

    private void ConfigureGrid()

    {

 

 

        var firstItem = ViewItems[0];

 

 

 

 

        foreach (string key in firstItem.Fields.Keys)

 

        {

 

 

            GridViewDataColumn col = new GridViewDataColumn();

 

 

 

            col.Header = key;

 

 

 

 

            Binding b = new Binding("Fields");

 

            b.Converter = 

 

new DataBindingConverter();

 

            b.ConverterParameter = key;

            b.Mode =

 

 

            BindingMode.TwoWay;

 

            col.IsSortable =

 

true;

 

            col.DataMemberBinding = b;

 

 

           this.TheGrid.Columns.Add(col);

 

        }

 

   }

 

 

 

 

    private void CreateDataSource()

 

    {

        ViewItems =

 

new ObservableCollection<ViewItem>();

 

 

 

 

       ViewItem item = new ViewItem();

 

 

        item.Fields.Add(

 

"ID", 5);

 

 

        item.Fields.Add(

 

"NAME", "A");

 

 

        item.Fields.Add(

 

"AGE", 3);

 

 

        ViewItems.Add(item);

 

 

       var item1 = new ViewItem();

 

 

        item1.Fields.Add(

 

"ID", 2);

 

 

        item1.Fields.Add(

 

"NAME", "G");

 

 

        item1.Fields.Add(

 

"AGE", 5);

 

 

        ViewItems.Add(item1);

 

 

       var item2 = new ViewItem();

 

 

        item2.Fields.Add(

 

"ID", 6);

 

 

        item2.Fields.Add(

 

"NAME", "K");

 

 

        item2.Fields.Add(

 

"AGE", 5);

 

 

        ViewItems.Add(item2);

 

 

   }
}

 

 

Quick help is really appreciated!

 

1 Answer, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 02 Dec 2010, 03:50 PM
Hello Gauri,

On examining your code-snippet, I may suggest you to try out the IsCustomSortingEnabled property of the column and set it to "True". However, please keep in mind that your collection should implement either IComparable Interface or IEquatable Interface in order to be able to perform sorting.

All the best,
Maya
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
Tags
GridView
Asked by
Gauri
Top achievements
Rank 1
Answers by
Maya
Telerik team
Share this question
or