0

I'm writing a small winform Windows application that retrieves information from a webshop. Retrieving the information works and I am able to show it in a dataGridView.

I retrieve the information in list named "orders", this list has multiple levels in structure. When I set "orders" as datasource for my dataGridView all information on level 0 is visible [Great].

All orders are displayed as rows in my dataGridView, but now I need to access information deeper in the list.

List "orders" contains all the orders and some basic information, but I need to display

orders.0.billing.firstname

Is there a way to also show columns deeper in order list?

Here is a screenshot of how orders is built :

enter image description here

This is the code:

 public async void RefreshOrders()
 {
     //Connect to domain using REST API
     MyRestAPI rest = new MyRestAPI("https://example.com/wp-json/wc/v3/", "ck_", "cs_");

     //Create object wc from store
     WCObject wc = new WCObject(rest);

     //Get all orders
     var orders = await wc.Order.GetAll();

     //Store all orders in a dataGridView
     dataGridViewOrders.DataSource = orders;
 }

It need to be like this:

enter image description here

orders -> order -> billing -> firstname :

orders > order > billing > firstname

Ramil Aliyev 007
  • 4,437
  • 2
  • 31
  • 47
Nunile
  • 1
  • 1
  • Related: [How to flatten nested objects with linq expression](https://stackoverflow.com/questions/6428940/) – Self Mar 02 '21 at 10:48
  • May you share the definition of orders. And create a class that represent the data the way you want to display. The transpormation may just be a simple select based on `orders.0.billing.firstname`. Like `var result = orders.Select(x=> new { nestedBilling = x.billing.firstname, orderInfo1 = x.OrderProperty1})` – Self Mar 02 '21 at 10:51
  • I inserted a picture of the definition. Each order is placed directly under "orders" Right now there are 4 orders. so 4 objects directly under "orders". Each order is a list of it's own, containing strings, but also more lists. "Billing" is a list within each order. In the billing list, firstname can be found. How do I flatten all lists within "orders" so all information is on 1 row in my dataGridView. Later I can than visible = false the unwanted. Or can I do this more simple? – Nunile Mar 02 '21 at 11:36
  • Click the linked question, see how it's clear with simple class definition with just the number of properties to show the behavior. Test the code `orders.Select(..`, Because what you call 4 level on nesting is just not that complexe.From debugger scrren shoot billing is a property of order so there is no list here. – Self Mar 02 '21 at 13:56
  • https://dotnetfiddle.net/mFqlty – Self Mar 02 '21 at 14:02
  • I am trying to understand the code in the dotnetfiddle. But I cannot seem to understand why you use 'orders' since this is the list that is filled on runtime with data from the webshop. – Nunile Mar 02 '21 at 15:00
  • For testing I do not have access to your database/service? So I just create `var orders =` like in your code. Orders is a list/array/collection of order. – Self Mar 02 '21 at 15:07
  • var orders = await wc.Order.GetAll(); fills 'orders' with all orders from the webshop (WooCommerce). I'm using using WooCommerceNET.WooCommerce.v3; to get these orders. So at runtime, var orders is filled automatically. The only thing I want is to flatten this list. This way all other information stored on level 1,2,3 etc levels deep, is showed. – Nunile Mar 02 '21 at 15:56
  • And ? You expect `var orders = await wc.Order.GetAll();` to compile and retrieve information on a online demo? no. Sop you don't expect me to build a demo other a line a code that won't execute. There must be a miss understanding. And it must be me . I can't see why you cant copy past the result line into your code and put result into the datable. – Self Mar 02 '21 at 16:07
  • Click the frist link on this comment tread. See how that question is clear with simple class definition. and can be easly scale up to any level of nesting . That an good question. If that and the demo does not cover your issue. you have to get back to [ask] and [mre] and get us some class definition. you are looping on "nested" and "filled at runtime.". But you are not giving the info needed to write any code. Bassically showing a picture of your car to a mecanic asking for a fix. Im not trying to be rude. I just can't get my point across. And can't formulate it any better. – Self Mar 02 '21 at 16:14
  • I finished reading all the links, but I think the conclusion is that my knowledge of c# is too little to provide clear answers to your questions, but also to use your example and learn from it to translate it to my own solution. I was already happy that the application runs and is able to show all my orders in a dataGridView. Maybe i should try to learn more about what is inside your code instead of trying to directly use it in my solution. Many thanks for your help. – Nunile Mar 02 '21 at 17:30

0 Answers0