Syncfusion Xamarin.Forms SfDataGrid – Binding to List<Dictionary<,>>

corresponding syncfusion forums post

Binding to Collection<Dictionary<,>> can be accomplished but doesn’t appear to be well documented… this WPF SfDataGrid doc gave the clue.

The basic trick is to set the SfDataGrid’s column.MappingName = “Fields[FieldName]”;
where Fields is a Dictionary property on your DictionaryWrapper class.

I couldn’t get List<Dictionary<string, object>> working directly without the wrapper class “hiding” the dictionary from what I think is an SfDataGrid bug. The app crash exception call stack ultimately winds up on an invalid Linq related get_Item() call.

Below is sample working code including “SimpleTable” wrapper for List and Newtonsoft type converter for deserializing Json “table” directly into this datastructure… this facilitates delivery of tabular resultsets from web api’s… see my SqlClientHelpers.ToJson method as one implementation that works succinctly within Azure Functions for example.

FYI, I believe there is also a bug with SfDataGrid column sorting logic when bound to this kind of Dictionary, a non-fatal exception fires. I worked around by implementing grid.SortColumnsChanging.

Sample Deserialize call:

var data = "[{\"Source\":\"Web\",\"Batch Id\":1}, {\"Source\":\"Manual\",\"Batch Id\":2}]";
var table = JsonConvert.DeserializeObject<SimpleTable>(data);

Binding sample with crucial MappingName syntax:

grid.ItemsSource = table;
grid.Columns.Add(new GridTextColumn()
{
  HeaderText = "Source",
  MappingName = "Vals[Source]" // **** HERE'S THE KICKER ****
});

SimpleTable.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace DataHelpers
{
  [JsonConverter(typeof(DictRow_DictDeserializer))]
  public class DictRow
  {
    public Dictionary<string, object> Vals { get; set; }
    public DictRow(Dictionary<string, object> dict) { Vals = dict; }
  }

  public class SimpleTable : List<DictRow>
  {
    public SimpleTable(IEnumerable<DictRow> list) : base(list) { }
  }

  public class DictRow_DictDeserializer : JsonConverter
  {
    public override bool CanRead => true;
    public override bool CanWrite => false;
    public override bool CanConvert(Type objectType) => objectType == typeof(Dictionary<string, object>);

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
      return new DictRow(serializer.Deserialize<Dictionary<string, object>>(reader));
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
      throw new NotSupportedException();
    }
  }
}

Linq Enumerable to another Collection

given devices is an IEnumberable<>

var nvc = devices.Aggregate(new NameValueCollection(), (seed, current) => { seed.Add("device", current.Id.ToString()); return seed; });

Json Array of objects to Dictionary, keyed (indexed) on chosen property

using Json.Net; //Newtonsoft
using Json.Net.Linq;

public class Device {
  public int Id {get; set;}
  public string Name {get; set;}
  public string Property {get; set;}
}

var json = @"[{id: 1, name: ""name1"", prop: ""prop1""}, {id: 2, name: ""name2"", prop: ""prop""}]";

var devicesDictionary = JArray.Parse(json).ToDictionary( i=>i["id"].Value<int>(), i=>i.ToObject<Device>() );

Json.Net Serialize Dictionary<Tkey, Tobject> to List<Tobject>

Motivation: Wanted convenience of a Dictionary on the server side MVC controller (for some key look-up based logic), yet send the same list of objects down to Knockout Ajax client, which most readily consumes lists as Javascript object arrays.

Could’ve just exposed the List<Object> as another property but wanted to see if I could roll it all into one property just to learn a little more about the Json.Net API.Nice that Json.Net’s tokenization framework has already abstracted away from specific types within the overridden method scope on custom JsonConverter class, so this approach generically handles *any* Dictionary<object, object> without any additional effort.

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace YouNameIt {

  using CartItemDict = Dictionary<long, CartItemDto>;

  //reverse of this: http://stackoverflow.com/questions/24759181/deserializing-a-json-dictionaryint-customtype-to-listcustomtype
  //adapted from here: http://james.newtonking.com/json/help/index.html?topic=html/CustomJsonConverter.htm
  public class DictToListConverter : JsonConverter
  {
    public override bool CanConvert(Type objectType)
    {
      return objectType == typeof (Dictionary<object, object>);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
      throw new NotImplementedException();
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
      var t = JToken.FromObject(value);

      if (t.Type != JTokenType.Object)
      {
        t.WriteTo(writer);
      }
      else
      {
        var o = (JObject) t;
        var values = o.Properties().Select(p => p.Value).ToList();
        (new JArray(values)).WriteTo(writer);
      }
    }
  }

  public class CartDto
  {
    [JsonConverter(typeof(DictToListConverter))]
    public CartItemDict Items { get; set; }

    [DataType(DataType.Currency)]
    public decimal TotalPrice { get; private set; }
  }

  public class CartItemDto : VariantDto
  {
    public long CartItemId { get; private set; }
    public int Quantity { get; private set; }

    [DataType(DataType.Currency)]
    public decimal UnitPrice { get; private set; }
    [DataType(DataType.Currency)]
    public decimal LinePrice { get; private set; }

    private List _addons;
    public List Addons { get { return _addons ?? (_addons = new List()); } }
  }
}