Xin chào các bạn, bài viết hôm nay mình sẻ hướng dẫn các bạn cách load dữ liệu Json Datasource vào GridView trên Devexpress Winform C#.

[DEVEXPRESS] Hướng dẫn load Json DataSource vào GridView

Dưới đây, là giao diện demo ứng dụng:
[DEVEXPRESS] Load data from json datasource to Gridview C#

Ở bài viết, này các bạn có đọc dữ liệu json từ file hoặc request từ web API.

Full source code C#:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DevExpress.DataAccess.Json;

namespace JsonBinddingGridView
{
    public partial class Form1 : DevExpress.XtraEditors.XtraForm
    {
        public Form1()
        {
            InitializeComponent();
           
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            // 1. Get json from file
            var jsonDataSource = new JsonDataSource();
            Uri fileUri = new Uri("data.json", UriKind.RelativeOrAbsolute);
            jsonDataSource.JsonSource = new UriJsonSource(fileUri);

            // 2. Get json from API Website
            //var jsonDataSource = new JsonDataSource();        
            //jsonDataSource.JsonSource = new UriJsonSource(
            //    new Uri("https://raw.githubusercontent.com/dinhtona/api-mssql-dapper/main/db.json"));

            jsonDataSource.Fill();
            gridControl1.DataSource = jsonDataSource;
        }
    }
}

Ở đoạn code trên các bạn có thể bỏ comment số 2, để chạy test load từ web API nhé.

Qua bài viết này các bạn có thể hiểu sơ bộ về việc lấy dữ liệu từ những file JSON như trên facebook chẳng hạn, để người dùng lấy id, email, họ tên, địa chỉ chẳng hạn vâ được in lên listbox hoặc listview để người dùng kiểm tra.
 
Hoặc có thể download bộ code bên dưới để tìm hiểu thêm nhé.

DOWNLOAD FULL SOURCE CODE

Chúc mọi người thành công với thủ thuật trên nhé.