Skip to content

Bind Json to a datagrid

New Discussion
Sanjay Shukla
Sanjay Shukla asked on Jun 16, 2015 2:30 PM

Hi There, I have a JSON output (from WCF service) that I need to bind to a data grid. The Json i am getting is the result of a query that is dynamic. Due to this reason, I am able to cast the Json into an object. I wanted to check if someone else has implemented this. Thanks Jay

Sign In to post a reply

Replies

  • 0
    Robert Stoffers
    Robert Stoffers answered on Oct 3, 2013 7:44 PM

    Hi Jay,

    The XamGrid requires some sort of IEnumerable of a known type in order to bind to it.  This means you need to create a class type that will contain the data inside the JSON string and give the grid a collection of this class.  For example, take the following JSON:

    [{"Name":"John Doe", "Email":"johnd@hotmail.com"},{"Name":"Mary Doe", "Email":"maryd@hotmail.com"}]

    This JSON has two objects containing a 'Name' and 'Email' field so we need a class to contain these properties. 

    public class Person
    {
        public string Name { get; set; }
        public string Email { get; set; }
    }

     

    You then need to parse the JSON and store the values in the Person class and then stick it inside a collection. 

    List<Person> people = ParseJSON(jsonString);
    
    xamGrid1.ItemsSource = people;

     

    Deserializing the JSON into a class can be done using the DataContractJsonSerializer.  This article discusses this: http://www.silverlightshow.net/items/JSON-serialization-and-deserialization-in-Silverlight.aspx

    • 0
      Sanjay Shukla
      Sanjay Shukla answered on Oct 3, 2013 10:29 PM

      Thanks for the feedback, the problem is that I dont have fixed output. if I had known that I am getting Person class then this apporach would work fine but in my case I dont have this knowledge. I might get a response that would have and ID and EMAIL but another result might return ID, LOCATION. Thanks Jay    

      • 0
        Robert Stoffers
        Robert Stoffers answered on Oct 4, 2013 4:50 PM

        Hi Jay,

        I looked into how you could handle this situation where you don’t know what the properties are in the objects and I came up with the following solution:

        Since you’re using Silverlight you should have access to the JsonValue class.  You need to add a reference to the System.Json assembly in order to get it.  Once you have this class, you can use the JsonValue.Parse method to parse the data into KeyValuePairs.  From these pairs you would need to create a column for the XamGrid and assign the Key property.  You can then directly set the JsonValue to the XamGrid.ItemsSource.  You must make sure that you set XamGrid.AutoGenerateColumns to false for this to work.  I’ve attached a sample that demonstrates this.

      • 0
        Robert Stoffers
        Robert Stoffers answered on Oct 9, 2013 10:33 PM

        Hi Jay,

        Let me know if you have any further questions on this matter.

      • 0
        Sanjay Shukla
        Sanjay Shukla answered on Oct 9, 2013 11:12 PM

        Hi Rob,

        I have another question for you, I get double quotes when adding the data in grid. What is teh best way to remove these double quotes. Please see the image attached. Appreciate your feedback.

        Thanks

        Jay   

      • 0
        Robert Stoffers
        Robert Stoffers answered on Oct 10, 2013 3:01 PM

        Hi Jay,

        To remove the quotes is a bit difficult.  The XamGrid doesn’t know how to handle a JsonValue so when it comes time to display the value it just calls ToString() on the JsonValue object.  This causes the value to convert back into JSON which is why the quotes are there.  I’ve altered the code a bit so the grid no longer binds to a JsonValue but a List<Dictionary<string, string>> instead.  The sample now iterates over the JsonValue result and stores all the data in this list.

      • 0
        Robert Stoffers
        Robert Stoffers answered on Oct 17, 2013 10:45 PM

        Hi Jay,

        Let me know if you have any further questions on this.

      • 0
        Sanjay Shukla
        Sanjay Shukla answered on Oct 18, 2013 6:44 PM

        Thanks Rob for your help. This is all I was looking for, you can close this request. Jay

      • 0
        Greg Kurpiel
        Greg Kurpiel answered on Jun 2, 2015 3:24 PM

        Hi Rob, 

        I had the same issued and followed your example worked great. However I have one further requirement. I need to add SUM and AVERAGE to my summaries when the column contains numeric values. Currently I only get count, maximum, minimum. I have enabled all the summaries options I can think of. Converted the numbers to decimals and rounded to two decimal places. Nothing I do shows the SUM and AVERAGE operands in the summaries list. Any chance you could update this example to also include summaries. 

        Thanks, 
        Greg 

      • 0
        Robert Stoffers
        Robert Stoffers answered on Jun 5, 2015 10:21 PM

        Hi Greg,

        The XamGrid only shows summaries that will work with the associated data type of the column.  In the case of this JSON sample, each column is dealing with string values so the summaries available are only the ones that can handle a string.  The Sum or Average summaries won't know what to do with a string.  So pretty much the only way to get a Sum or Average summary to show up is to either provide a strongly typed object which will only work if you know what JSON you are getting, or you can create a custom summary and do the calculation yourself, converting the JSON value into the appropriate datatype.  I updated the sample to provide a custom summary.

        Please keep in mind that the sample was made 2 years ago in VS2010 but I no longer have 2010 installed on my machine, only VS 2013.  So this is what the sample is built in.  I also made use of the Json.NET library to make deserializing the json a bit easier.

      • 0
        Greg Kurpiel
        Greg Kurpiel answered on Jun 8, 2015 4:00 PM

        Thanks Rob. Works great!! I just needed to account for the zeros. 

      • 0
        Robert Stoffers
        Robert Stoffers answered on Jun 16, 2015 2:30 PM

        Hi Greg,

        Awesome!  I'm glad that helped.

  • You must be logged in to reply to this topic.
Discussion created by
Favorites
Replies
Created On
Last Post
Discussion created by
Sanjay Shukla
Favorites
0
Replies
13
Created On
Jun 16, 2015
Last Post
10 years, 8 months ago

Suggested Discussions

Created by

Created on

Jun 16, 2015 2:30 PM

Last activity on

Feb 20, 2026 12:00 PM