C# Flurl HTTP - Download JSON,Fill ListView


C# HTTP Flurl ListView JSON Tutorial Example.

In this tutorial we see a practical example of how to download and parse JSON data from online and bind the data in a C# windows forms listview.

Video Tutorial

Here's the video tutorial version of this class.

Demo

C# ListView HTTP FLurl

Program.cs

Here's the full Program.cs file.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Flurl.Http;

namespace JSONListView
{ /*
     * User class. A POCO class.
     */
    class User
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
    }
    class Program
    {
        private static SynchronizationContext synchronizationContext;
        private static ListView myListView;
        private static Label progressLabel;
        /*
         * Setup ListView. Instantiate, then add columns then register the ItemSelectionChanged events
         */
        private static void setupListView()
        {
            myListView = new ListView { Location = new Point(30, 70), Size = new Size(400, 400), View = View.Details, FullRowSelect = true, Alignment = ListViewAlignment.SnapToGrid };
            myListView.Columns.Add("ID", 100);
            myListView.Columns.Add("NAME", 150);
            myListView.Columns.Add("EMAIL", 150);

            myListView.ItemSelectionChanged += myListView_ItemSelectionChanged;

            progressLabel = new Label()
            {
                Location = new Point(50, 50),
                ClientSize = new Size(200, 15),
                ForeColor = Color.AliceBlue,
                Text = "Downloading data.........Please wait"
            };
        }
        /*
         * Listen to ItemSelectionChange events for our ListView.
         * Then show the name in a messagebox
         */
        static void myListView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            ListViewItem listViewItem = e.Item;
            String name = listViewItem.SubItems[1].Text;
            if (e.IsSelected)
            {
                MessageBox.Show(name,"ListView JSON",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }
        /*
         * Download json data from online using Http Flurl. Do it asynchronously.
         */
        private static async void downloadJSONData()
        {
            try
            {
                //PERFORM IN BACKGROUND
                await Task.Run(async () =>
                {
                    List<User> users = await "https://jsonplaceholder.typicode.com/users".GetJsonAsync<List<User>>();
                    foreach (User user in users)
                    {
                        synchronizationContext.Post(value =>
                        {
                            var mUser = (User)value;
                            progressLabel.Text = "Download Complete.";
                            myListView.Items.Add(new ListViewItem(new[] { mUser.Id, mUser.Name, mUser.Email }));

                        }, user);
                    }
                });
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        /*
     * Create a Form, Set its properties and run the application.
     */
        private static void createForm()
        {
            synchronizationContext = SynchronizationContext.Current;

            Form myForm = new Form
            {
                Text = "C# Flurl HTTP : ListView - Fill From JSON",
                ClientSize = new Size(564, 520),
                BackColor = Color.MediumSeaGreen
            };

            myForm.Controls.Add(progressLabel);
            myForm.Controls.Add(myListView);
            Application.EnableVisualStyles();
            Application.Run(myForm);
        }
        /*
        * Main method
        */
        public static void Main()
        {
            setupListView();
            downloadJSONData();
            createForm();
        }
    }
}

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think


Previous Post Next Post