Hi
I want to display below data in Repeater/Gridview. Data is returned by Api response. I am using RestSharp
C#
{"status":true,"data":{"id":"16621681","order_id":"29920015","order_number":"890","created":"2022-12-26","awb_number":"14189990126365","rto_awb":"","courier_id":"3","warehouse_id":"132002","rto_warehouse_id":"132002","status":"cancelled","rto_status":"","shipment_info":"","history":[{"status_code":"PP","location":"CAR\/HUB, Chandigarh, CHANDIGARH","event_time":"2022-12-26 19:37","message":"Pickup Not Done"},{"status_code":"PP","location":"CAR\/HUB, Chandigarh, CHANDIGARH","event_time":"2022-12-26 12:10","message":"Out for Pickup"},{"status_code":"PP","location":"CAR\/HUB, Chandigarh, CHANDIGARH","event_time":"2022-12-26 11:17","message":"Pending"}]}}Thanks

Amit MohantyRank #112 · 20K reputationPosted Dec 28, 2022, 12:12 AM EST
Please debug your code while load data to rootObject and check history node has data or not. In my below code there is two gridview one for root data and other for history object data.
Jignesh KumarRank #91 · 22.9K reputationPosted Dec 27, 2022, 1:19 AM EST
Hi Ramco,
Please refer these article which will help you out for gridview binding,
https://www.c-sharpcorner.com/article/how-to-convert-and-bind-json-string-in-to-asp-net-gridview-with-c-sharp/
Amit MohantyRank #112 · 20K reputationPosted Dec 27, 2022, 6:26 AM EST
Check this
function divexpandcollapse(divname) { var div = document.getElementById(divname); var img = document.getElementById('img' + divname); if (div.style.display == "none") { div.style.display = "inline"; img.src = "Content/images/minus.png"; } else { div.style.display = "none"; img.src = "Content/images/plus.png"; } }Code
using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Data; using System.Reflection; using System.Web.UI.WebControls; namespace MyWebApp { public partial class NestedGridView : System.Web.UI.Page { RootObject rootObject = new RootObject(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GridView1.DataSource = GetData(); GridView1.DataBind(); } } private DataTable GetData() { string json = "your json data"; rootObject = JsonConvert.DeserializeObject(json); List orderDetails = new List(); orderDetails.Add(rootObject.data); DataTable dataTable = ToDataTable(orderDetails); return dataTable; } protected void OnRowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { GridView GridView2 = e.Row.FindControl("GridView2") as GridView; GridView2.DataSource = ToDataTable(rootObject.data.history); GridView2.DataBind(); } } public DataTable ToDataTable(List items) { DataTable dataTable = new DataTable(typeof(T).Name); PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo prop in Props) dataTable.Columns.Add(prop.Name); foreach (T item in items) { var values = new object[Props.Length]; for (int i = 0; i history { get; set; } } public class ShipmentHistory { public string status_code { get; set; } public string location { get; set; } public string event_time { get; set; } public string message { get; set; } } }Amit MohantyRank #112 · 20K reputationPosted Dec 28, 2022, 5:01 AM EST
if history object has data then assign it to the gridview in which you want to show data.
Suppose you want to show data in GridView3 then try like below code.
Ramco RamcoRank #462 · 5.4K reputationPosted Dec 27, 2022, 1:10 PM EST
Hi AMit
I am getting this error -
dynamic json = JsonConvert.DeserializeObject(response.Content); GridView1.DataSource = GetData(json); GridView1.DataBind(); } private DataTable GetData(string jsondata) { string json = jsondata; rootObject = JsonConvert.DeserializeObject(json); List orderDetails = new List(); orderDetails.Add(rootObject.data); DataTable dataTable = ToDataTable(orderDetails); return dataTable; }Below is the response returned
{"status":true,"data":{"id":"16621681","order_id":"29920015","order_number":"890","created":"2022-12-26","edd":"","pickup_date":"","rto_initiate_date":"","delivered_date":"","shipped_date":"","awb_number":"14189990126365","rto_awb":"","courier_id":"3","warehouse_id":"132002","rto_warehouse_id":"132002","status":"cancelled","rto_status":"","shipment_info":"","history":[{"status_code":"PP","location":"CAR\/HUB, Chandigarh, CHANDIGARH","event_time":"2022-12-26 19:37","message":"Pickup Not Done"},{"status_code":"PP","location":"CAR\/HUB, Chandigarh, CHANDIGARH","event_time":"2022-12-26 12:10","message":"Out for Pickup"},{"status_code":"PP","location":"CAR\/HUB, Chandigarh, CHANDIGARH","event_time":"2022-12-26 11:17","message":"Pending"}]}}Thanks
Amit MohantyRank #112 · 20K reputationPosted Dec 27, 2022, 11:30 PM EST
Try this
Jignesh KumarRank #91 · 22.9K reputationPosted Dec 27, 2022, 11:48 PM EST
Please refer you previous question , https://www.c-sharpcorner.com/Forums/error-invalid-arguments
Ramco RamcoRank #462 · 5.4K reputationPosted Dec 27, 2022, 11:53 PM EST
Hi Amit
With this it is not display History information. Kindly see the response generated
{"status":true,"data":{"id":"16621681","order_id":"29920015","order_number":"890","created":"2022-12-26","edd":"","pickup_date":"","rto_initiate_date":"","delivered_date":"","shipped_date":"","awb_number":"14189990126365","rto_awb":"","courier_id":"3","warehouse_id":"132002","rto_warehouse_id":"132002","status":"cancelled","rto_status":"","shipment_info":"","history":[{"status_code":"PP","location":"CAR\/HUB, Chandigarh, CHANDIGARH","event_time":"2022-12-26 19:37","message":"Pickup Not Done"},{"status_code":"PP","location":"CAR\/HUB, Chandigarh, CHANDIGARH","event_time":"2022-12-26 12:10","message":"Out for Pickup"},{"status_code":"PP","location":"CAR\/HUB, Chandigarh, CHANDIGARH","event_time":"2022-12-26 11:17","message":"Pending"}]}}Thanks
Ramco RamcoRank #462 · 5.4K reputationPosted Dec 28, 2022, 4:28 AM EST
Hi Amit
Yes in rootObject it is showing . In history it is showing 3 records.
Thanks