In this article I will explain with an example, how to bind (populate) DataGridView with JSON data from JSON file in Windows Forms Application using C# and VB.Net.
The JSON file will be read from disk Folder (Directory) and converted to DataTable using JSON.Net library.
Finally, the DataTable will be used to populate (bind) DataGridView in Windows Forms Application using C# and VB.Net.
Download JSON.Net library
The JSON.Net library is available for download from the following URL.
The JSON string
Following is the JSON string saved inside a JSON file in the disk Folder (Directory) which will be used to populate the DataGridView.
[
{
"Id":1,
"Name":"John Hammond",
"Country":"United States"
},
{
"Id":2,
"Name":"Mudassar Khan",
"Country":"India"
},
{
"Id":3,
"Name":"Suzanne Mathews",
"Country":"France"
},
{
"Id":4,
"Name":"Robert Schidner",
"Country":"Russia"
}
]
Form Design
The following Form consists of a DataGridView control.
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using System.Data;
using Newtonsoft.Json;
VB.Net
Imports System.IO
Imports System.Data
Imports Newtonsoft.Json
Populating DataGridView using C# and VB.Net
Inside the Form Load event, the contents of the JSON file are read using ReadAllText method of File class from the disk Folder (Directory) and converted to DataTable using JSON.Net library.
Finally, the DataTable is used to populate the DataGridView.
C#
private void Form1_Load(object sender, EventArgs e)
{
string json = File.ReadAllText(@"E:\Customers.json");
dataGridView1.DataSource = JsonConvert.DeserializeObject<DataTable>(json);
}
VB.Net
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim reader As String = File.ReadAllText("E:\Customers.json")
dataGridView1.DataSource = JsonConvert.DeserializeObject(Of DataTable)(json)
End Sub
Screenshot
Downloads