In this article I will explain with an example, how to bind DataGridView using JSON from URL in Windows Forms (WinForms) Applications using C# and VB.Net
Download JSON.Net library
The JSON.Net library is available for download from the following URL.
The JSON string returned from the API
The following JSON file will be used in this article.
[
{
"CustomerId": 1,
"Name": "John Hammond",
"Country": "United States"
},
{
"CustomerId": 2,
"Name": "Mudassar Khan",
"Country": "India"
},
{
"CustomerId": 3,
"Name": "Suzanne Mathews",
"Country": "France"
},
{
"CustomerId": 4,
"Name": "Robert Schidner",
"Country": "Russia"
}
]
Form Design
The following Form consists of a DataGridView Control.
Namespaces
C#
using System.Net;
using Newtonsoft.Json;
VB.Net
Imports System.Net
Imports Newtonsoft.Json
Bind DataGridView using JSON from URL in C# and VB.Net
Inside the Form Load event, the JSON string is downloaded from an URL using DownloadString method and converted to DataTable using JSON.Net library.
Finally, the DataTable is assigned to the DataSource property of the DataGridView.
C#
private void Form1_Load(object sender, EventArgs e)
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
string json = (new WebClient()).DownloadString("https://raw.githubusercontent.com/aspsnippets/test/master/Customers.json");
dataGridView1.DataSource = JsonConvert.DeserializeObject<DataTable>(json);
}
VB.Net
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
ServicePointManager.Expect100Continue = True
ServicePointManager.SecurityProtocol = CType(3072, SecurityProtocolType)
Dim json As String = (New WebClient()).DownloadString("https://raw.githubusercontent.com/aspsnippets/test/master/Customers.json")
DataGridView1.DataSource = JsonConvert.DeserializeObject(Of DataTable)(json)
End Sub
Screenshot
Downloads