Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind datagrid using third party API datasource

Tags:

c#

winforms

I am using C# 4.5. I have created a windows application.

As per the project requirement, I need to bind a third-party api response that returns array of json objects into a datagrid.

I've fetched data using RestClient and have converted them to object using JDynamic package as

IRestResponse response = client.Execute(request);                 
dynamic result = new JDynamic(response.Content);

Is there any technique I may bind this array of objects as a datasource of gridview?

Also, what if I need to display only certain columns?

Edit:


The JSON response is something like:

[
    {
        "id": 7,
        "username": "[email protected]",
        "first_name": "Carlos",
        "middle_name": "",
        "last_name": "Alaiz",
        "gender": "male",
        "addr1": "",
        "addr2": "",
        "state": "",
        "city": "",
        "country": "",
        "post_code": "",
        "email_id": "[email protected]",
        "email_id_alt": "",
        "phone1": "1800777333",
        "phone2": "",
        "photo": "2015-09-09-17-14-29-2015-01-22-06-26-15-400-don-passport-lighting-test 044-3-100pc_pp-ps.jpg",
        "populations": [{
            "id": 8,
            "population_name": "Pop 8"
        }]
    },
    {
        "id": 8,
        "username": "[email protected]",
        "first_name": "Pedro",
        "middle_name": "",
        "last_name": "Montero",
        "gender": "male",
        "addr1": "",
        "addr2": "",
        "state": "",
        "city": "",
        "country": "",
        "post_code": "",
        "email_id": "[email protected]",
        "email_id_alt": "",
        "phone1": "1800999222",
        "phone2": "",
        "photo": "2015-09-09-17-13-40-2014-10-27-06-59-56-bilde.jpeg",
        "populations": [{
            "id": 8,
            "population_name": "Pop 8"
        }]
    },
    {
        "id": 9,
        "username": "[email protected]",
        "first_name": "Mario",
        "middle_name": "",
        "last_name": "Sanz",
        "gender": "male",
        "addr1": "",
        "addr2": "",
        "state": "",
        "city": "",
        "country": "",
        "post_code": "",
        "email_id": "[email protected]",
        "email_id_alt": "",
        "phone1": "1800288299",
        "phone2": "",
        "photo": "2015-09-09-17-13-17-2014-10-27-06-28-30-Jim-Smith.jpg",
        "populations": [{
            "id": 7,
            "population_name": "Pop 7"
        }]
    },
    {
        "id": 10,
        "username": "[email protected]",
        "first_name": "Parikshit",
        "middle_name": "",
        "last_name": "Thakur",
        "gender": "male",
        "addr1": "",
        "addr2": "",
        "state": "",
        "city": "",
        "country": "",
        "post_code": "",
        "email_id": "[email protected]",
        "email_id_alt": "",
        "phone1": "1800299200",
        "phone2": "",
        "photo": "2015-09-10-11-52-05-parikshit.jpg",
        "populations": [{
            "id": 8,
            "population_name": "Pop 8"
        }]
    },
    {
        "id": 11,
        "username": "[email protected]",
        "first_name": "Nidhi ",
        "middle_name": "",
        "last_name": "Patel",
        "gender": "female",
        "addr1": "",
        "addr2": "",
        "state": "",
        "city": "",
        "country": "",
        "post_code": "",
        "email_id": "[email protected]",
        "email_id_alt": "",
        "phone1": "1800200300",
        "phone2": "",
        "photo": "female.jpg",
        "populations": []
    }
]

I have created a new class for it as below:

namespace DemoAutoLogin
{
    public class patient
    {
        public int id { get; set; }
        public string username { get; set; }
        public string first_name { get; set; }
        public string middle_name { get; set; }
        public string last_name { get; set; }
        public string gender { get; set; }
        public string addr1 { get; set; }
        public string addr2 { get; set; }
        public string state { get; set; }
        public string city { get; set; }
        public string country { get; set; }
        public string post_code { get; set; }
        public string email_id { get; set; }
        public string email_id_alt { get; set; }
        public string phone1 { get; set; }
        public string phone2 { get; set; }
        public string photo { get; set; }
        public List<Population> populations { get; set; }
    }

    public class Population
    {
        public int id { get; set; }
        public string population_name { get; set; }
    }
}

Now, what can I do to display the JSON records in grid view?

like image 212
Dev Avatar asked Jun 30 '26 12:06

Dev


1 Answers

You then simply can deserialize from result to the IEnumerable<patient>. Use Json.NET for this. the IEnumerable<patient> you can bind to the grid.

public void BindMyData()
{
    IEnumerable<patient> patients = JsonConvert.DeserializeObject<IEnumerable<patient>>(resultAsJson);
    datagrid.DataSource = patients;
}

Should work this way..

if you don't have a poco:

public void BindMyData()
{
    IEnumerable<Dictionary<string, object>> patients = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(resultAsString);
    // if this works, i'm not sure:
    datagrid.DataSource = patients;
}

if you can't bin the IEnumerable<Dictionary<string, object>> to the grid, you maybe could bind a DataTable. Add this ToDataTable extension:

public static class Exts
{
    public static DataTable ToDataTable(this IEnumerable<Dictionary<string, object>> list)
    {
        DataTable result = new DataTable();
        if (!list.Any())
            return result;

        var columnNames = list.SelectMany(dict => dict.Keys).Distinct();
        result.Columns.AddRange(columnNames.Select(c => new DataColumn(c)).ToArray());
        foreach (var item in list)
        {
            var row = result.NewRow();
            foreach (var key in item.Keys)
                row[key] = item[key];

            result.Rows.Add(row);
        }

        return result;
    }

}

and then bind it this way:

public void BindMyData()
{
    IEnumerable<Dictionary<string, object>> patients = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(resultAsString);
    datagrid.DataSource = patients.ToDataTable();
}
like image 195
Matthias Burger Avatar answered Jul 03 '26 01:07

Matthias Burger