In case helpful for others searching here... binding to Collection<Dictionary<,>> can be accomplished but doesn't appear to be well documented... this WPF SfDataGrid doc gave clue.
The basic trick is to set the SfDataGrid's column.MappingName = "Fields[FieldName]";
where Fields is a Dictionary property on your List
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.
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();
}
}
}
I am also interested in "sample for Knowledge Base with sorting,grouping,filtering,etc". Please suggest date to get this available,if you can.
Thanks for the sample! The need for GridQueryableCollectionViewWrapper looks to be very enlightening.
FYI for other folks copying this code, the JsonConverter approach in the code I provided looks to save an extra iteration through the rows of Json data vs the PopulateData method in the Syncfusion sample.
Thanks. Please check attached sample where you will get the mentioned issue. I have crossed check your provided sample this work great but for my sample it crashes.