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 ****


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

namespace DataHelpers
  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();

4 Replies to “Syncfusion Xamarin.Forms SfDataGrid – Binding to List<Dictionary<,>>”

  1. It is really great post. But sorting, grouping, filtering,etc not working in sfdatagrid in above case. will be great help if you suggest/post code to achieve them. It is very difficult to implement above feature row by row in grid.

      1. awesome! they finally posted the missing bits for sorting just this morning … the trick is implementing GridQueryableCollectionViewWrapper around the list of dictionary rows, which apparently shows the grid how to properly retrieve values from this data structure… very cool to see how this hangs together

    1. awesome! they finally just posted the sorting sample code this morning… the trick is wrapping the list of dictionaries with a special “GridQueryableCollectionViewWrapper” which clearly provides the grid with a standardized way to retrieve values from custom data-structures like this… very enlightening to see how this hangs together

Leave a Reply