C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe -reference:Newtonsoft.Json.dll /target:winexe .\Table.cs
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe -r:Newtonsoft.Json.dll /target:winexe .\Table.cs
| using System; | |
| using System.IO; | |
| using System.Collections.Generic; | |
| using System.Net; | |
| using System.Text; | |
| using System.Diagnostics; | |
| using System.Linq; | |
| using Newtonsoft.Json; | |
| using Newtonsoft.Json.Linq; | |
| using System.Drawing; | |
| using System.Windows.Forms; | |
| public class Program : Form | |
| { | |
| //Esta funcion se escribe sola en Visual Studio | |
| //Declaro elementos visuales | |
| private DataGridView productGrid = new DataGridView(); | |
| private Button btnLoad = new Button(); | |
| private Panel buttonPanel = new Panel(); | |
| public Program() | |
| { | |
| //Esta funcion se escribe sola en Visual Studio | |
| //Definir evento que dispare cuando la ventana ha iniciado | |
| this.Load += new EventHandler(Program_Load); | |
| } | |
| private void Program_Load(System.Object sender, System.EventArgs e) | |
| { | |
| //Esta funcion se escribe sola en Visual Studio | |
| //Agrego boton y su panel correspondiente | |
| SetupLayout(); | |
| DrawTable(); | |
| } | |
| private void SetupLayout() { | |
| //Esta funcion se escribe sola en Visual Studio | |
| //Definimos size de la ventana | |
| this.Size = new Size(600, 500); | |
| //Seteamos el texto del boton | |
| btnLoad.Text = "Cargar datos"; | |
| //Ubicamos al boton en la ventana | |
| btnLoad.Location = new Point(10, 10); | |
| //Adding un evento on click | |
| btnLoad.Click += new EventHandler(btnLoad_Click); | |
| buttonPanel.Controls.Add(btnLoad); | |
| buttonPanel.Height = 50; | |
| buttonPanel.Dock = DockStyle.Bottom; | |
| this.Controls.Add(this.buttonPanel); | |
| } | |
| private void DrawTable() { | |
| //Esta funcion se escribe sola en Visual Studio | |
| //Add table a la lista de controles de la ventana | |
| this.Controls.Add(productGrid); | |
| //Indico el numero de columnas | |
| productGrid.ColumnCount = 2; | |
| //Defino la posicion en la ventana | |
| productGrid.Location = new Point(8, 8); | |
| //Defino el size de la tabla | |
| productGrid.Size = new Size(this.Size.Width - 30, this.Size.Height); | |
| //Defino nombres de columnas | |
| productGrid.Columns[0].Name = "Id"; | |
| productGrid.Columns[1].Name = "Producto"; | |
| } | |
| private void btnLoad_Click(object sender, EventArgs e) | |
| { | |
| MakeRequest(); | |
| } | |
| private void MakeRequest() { | |
| //Esto lo escribe el programador | |
| //Consume datos de la nube | |
| string URL = "http://1ce90d65de33.ngrok.io/MProducto"; | |
| HttpWebRequest request = (HttpWebRequest) WebRequest.Create(URL); | |
| request.ContentType = "application/json; charset=utf-8"; | |
| HttpWebResponse response = request.GetResponse() as HttpWebResponse; | |
| using (Stream responseStream = response.GetResponseStream()) | |
| { | |
| StreamReader reader = new StreamReader(responseStream, Encoding.UTF8); | |
| JObject o = JObject.Parse(reader.ReadToEnd()); | |
| JArray jsonarray = (JArray)o["objects"]; | |
| String name; | |
| int id; | |
| for (int i = 0; i < jsonarray.Count; i++) | |
| { | |
| JObject aItem = (JObject)jsonarray[i]; | |
| id = (int)aItem["nIdProducto"]; | |
| name = (string)aItem["cNombreProducto"]; | |
| InsertRow(id, name); | |
| } | |
| } | |
| } | |
| private void InsertRow(int id, string name) { | |
| //Esto lo escribe el programador | |
| string[] row0 = { id + "", name}; | |
| productGrid.Rows.Add(row0); | |
| } | |
| public static void Main() | |
| { | |
| Application.Run(new Program()); | |
| } | |
| } |