Web API to Data Grid View (Winform)
Hi guys, I'm trying to make a web api and throw the data in the thing in my data grid view in my winform project.
But I was taught the old ancient way, please tell me the new way.
(I'm having an exam soon T-T)
21 Replies
where
T
is a class describing what the data you get looks likeSo what's data's type here ?
Whatever describes the JSON you get from the API
If you get, say,
you would use something like
And do
There are ways to generate classes from JSON, too, so that makes things easier
$jsongen
Instantly parse JSON in any language | quicktype
Whether you're using C#, Swift, TypeScript, Go, C++ or other languages, quicktype generates models and helper code for quickly and safely reading JSON in your apps. Customize online with advanced options, or download a command-line tool.
Convert JSON to C# Classes Online - Json2CSharp Toolkit
Convert any JSON object to C# classes online. Json2CSharp is a free toolkit that will help you generate C# classes on the fly.
So I have these classes
Then I do this ...
I'll get
{
"ID" = "1",
"Name" = "Johnson",
"Adress" = "New York",
"Phone" = "12345678",
"ClassID" = "101",
"Portrait " = "Uglyface.png"
}
?
Sinhvien
instead of Root
, I assume?
And... is that data incorrect?After getting that, how do I show it on the Datagrid ?
Create a
DataSet
, bind the Datagrid to it, add data to the DataSet
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.datagrid?view=netframework-4.8.1#examples here's an exampleHey I'm reading this
And I wonder what's that
Like the type of the variable ?
You mean
var
?
It's type inferenceI know what's var is but that line
What's the use of it ?
Since
JsonConvert.DeserializeObject<List<JsonResult>>()
returns List<JsonResult>
, then var
represents type List<JsonResult>
The use is to get the result of deserialization?
What do you mean?
How else would you get the deserialized data from the method?I mean the result of deserialization, is it a string or a list or ... what ?
That why I was confused with why use instead of other things ? .-.
Oh so its a list ?
DeserializeObject
is a generic method
With a signature like
So whatever type you pass as T
, you get out of itOh ...
Okay
Thanks
Hey um what are these lines for ?
Seems like they clear the request's accept headers, and add a new accept header telling the request to accept json data
GetAaJsonAsync()
already does that allHey I've successfully called the API and show the data on the data grid
Thanks for your help
Now I gotta do Add, Delete and Edit the Json thingy