We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Sorting grid with Dictionary backing throwing an error in 15.2

My grid's datasource is a Dictionary<string, object> which based on an example, I have wrapped in my DynamicModel class.  So my ViewModel looks something like this:

public class ViewModel : NotificationObject{ 
                ...
     public IncrementalList<DynamicModel> DynamicCollection;
     ... 
} 

and my wrapper for my Dictionary<string,object> looks like this:

public class DynamicModel : INotifyPropertyChanged{ 
                    public Dictionary<string, object> _values;        
                    ...
        public Dictionary<string, object> Values{
            get {
                return _values;
            }
            set {
                _values = value;
            }

        }
        public DynamicModel(){
            this._values = new Dictionary<string, object>();
       }

When I create a GridTextColumn, the mapping is set up like this:

MappingName = "Values[" + colProperty + "]"

Where colProperty is the key for the value in the dictionary.

This was all working fine in version 15.1, but when I update to 15.2, sorting no longer works. Now, when I attempt to sort, it throws an error: 


System.ArgumentException: 'Values[FILENAME]' is not a member of type 'IosGold.DynamicModel'

  at System.Linq.Expressions.Expression.PropertyOrField (System.Linq.Expressions.Expression expression, System.String propertyOrFieldName) [0x00097] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.8.0.175/src/mono/mcs/class/dlr/


Is there something that I need to change to make this compatible again, or is this just a bug in this version of the library?

1 Reply

SS Sivaraman Sivagurunathan Syncfusion Team May 29, 2017 11:15 AM UTC

Hi Clint Anderson, 
 
Thanks for your update. 
 
We are able to reproduce the issue in our side and is because we have changed the behavior of the sorting internally. Earlier we will be applying sorting based on the values in the columns but now we have changed it based on the objects of the columns to facilitate custom filtering and sorting comparing the row data objects. However you can overcome the sorting issue in sample level by overriding the GridQueryableCollectionViewWrapper and setting the ItemSource for the grid as shown in the below code example. 
 
Note: 
 
You need to set the ItemSource for the datagrid after populating data to the underlying collection as shown in the below code example. 
 
Work around: 
 
//QueryableViewExt.cs 
public class QueryableViewExt : GridQueryableCollectionViewWrapper 
{ 
    public QueryableViewExt(IEnumerable source, SfDataGrid grid) : base(source, grid) 
    { 
 
    } 
 
    public override Expression<Func<string, object, object>> GetExpressionFunc(string propertyName, DataOperation operation = DataOperation.Default, DataReflectionMode reflectionMode = DataReflectionMode.Default) 
    { 
        Expression<Func<string, object, object>> exp = base.GetExpressionFunc(propertyName, operation, reflectionMode); 
        if (exp == null) 
        { 
            Func<string, object, object> func; 
            func = (propertyname, record) => 
             { 
                 var provider = this.GetPropertyAccessProvider(); 
                 return provider.GetValue(record, propertyName); 
             }; 
            exp = (propertyname, record) => func(propertyName, record); 
        } 
        return exp; 
    } 
} 
 
 
//MainActivity.cs 
 
public GettingStartedViewController () 
{ 
    grid = new SfDataGrid(); 
            viewModel = new ViewModel(); 
            grid.AllowSorting = true; 
    grid.AutoGenerateColumns = false; 
    grid.ColumnSizer = ColumnSizer.Star; 
    viewModel.PopulateData(); 
    grid.ItemsSource = new QueryableViewExt(viewModel, grid); 
            grid.GridTapped += Grid_GridTapped; 
    grid.Columns.Add(new GridTextColumn() 
    { 
        HeaderText = "Order ID", 
        MappingName = "Values[orderID]", 
        LineBreakMode = UILineBreakMode.WordWrap, 
        TextAlignment = UITextAlignment.Left, 
        HeaderTextAlignment = UITextAlignment.Center, 
    }); 
 
    grid.Columns.Add(new GridTextColumn() 
    { 
        HeaderText = "Customer ID", 
        MappingName = "Values[customerID]", 
        LineBreakMode = UILineBreakMode.WordWrap, 
        TextAlignment = UITextAlignment.Left, 
        HeaderTextAlignment = UITextAlignment.Center, 
    }); 
 
    grid.Columns.Add(new GridTextColumn() 
    { 
        HeaderText = "First Name", 
        MappingName = "Values[firstName]", 
        LineBreakMode = UILineBreakMode.WordWrap, 
        TextAlignment = UITextAlignment.Left, 
        HeaderTextAlignment = UITextAlignment.Center, 
    }); 
 
    grid.Columns.Add(new GridTextColumn() 
    { 
        HeaderText = "Last Name", 
        MappingName = "Values[lastName]", 
        LineBreakMode = UILineBreakMode.WordWrap, 
        TextAlignment = UITextAlignment.Left, 
        HeaderTextAlignment = UITextAlignment.Center, 
    }); 
 
    grid.Columns.Add(new GridTextColumn() 
    { 
        HeaderText = "City", 
        MappingName = "Values[city]", 
        LineBreakMode = UILineBreakMode.WordWrap, 
        TextAlignment = UITextAlignment.Left, 
        HeaderTextAlignment = UITextAlignment.Center, 
    }); 
    grid.Columns.Add(new GridTextColumn() 
            { 
                        HeaderText = "GroupProperty", 
                        MappingName = "GroupProperty", 
                        Width = 0 
            }); 
    viewModel.RefreshGroup("firstName"); 
    this.View.Add(grid);             
} 
 
 
We have modified your sample which contains the fix for the reported issue and you can download the same from the below link. 
 
Regards,

Sivaraman 


Loader.
Live Chat Icon For mobile
Up arrow icon