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!