Note that there are some explanatory texts on larger screens.

plurals
  1. POProper way to edit multi-level nested list of objects
    primarykey
    data
    text
    <p>I have 3 objects:</p> <pre><code>public class Person { public int Id { get; set; } public string Name { get; set; } public string Surname { get; set; } public List&lt;Order&gt; Orders { get; set; } public Person() { Orders= new List&lt;Order&gt;(); } } public class Order { public int Id { get; set; } public string Description { get; set; } public string Date { get; set; } public List&lt;Item&gt; Items { get; set; } public Order() { Items= new List&lt;Item&gt;(); } } public class Item { public int Id { get; set; } public string ProductName { get; set; } public int Number { get; set; } } </code></pre> <p>As You can see every person can have multiple orders and every order can have multiple items.</p> <p>In my application data that comes from DB looks like so:</p> <pre><code>private List&lt;Person&gt; _persons; _persons = new List&lt;Person&gt; { new Person { Id = 1, Name = "John", Surname = "Smith", Orders = new List&lt;Order&gt; { new Order { Id = 1, Description = "First Order", Date = "2013-03-07", Items = new List&lt;Item&gt; { new Item {Id = 1, Number = 2, ProductName = "Chair"}, new Item {Id = 2, Number = 1, ProductName = "Bed"} } }, new Order { Id = 2, Description = "Second", Date = "2013-03-07", Items = new List&lt;Item&gt; { new Item {Id = 1, Number = 2, ProductName = "Pen"}, new Item {Id = 2, Number = 1, ProductName = "Pencil"} } } } }, new Person { Id = 2, Name = "Adam", Surname = "West", Orders = new List&lt;Order&gt; { new Order { Id = 1, Description = "Adams order", Date = "2013-03-07", Items = new List&lt;Item&gt; { new Item {Id = 1, Number = 2, ProductName = "first"}, new Item {Id = 2, Number = 1, ProductName = "second"} } }, new Order { Id = 2, Description = "Adams second", Date = "2013-03-07", Items = new List&lt;Item&gt; { new Item {Id = 1, Number = 2, ProductName = "Pen"}, new Item {Id = 2, Number = 1, ProductName = "Pencil"} } } } } }; </code></pre> <p>I've created custom User control with 2 labels and datagridview like this: <img src="https://i.stack.imgur.com/Az1me.png" alt="enter image description here"> below is my code:</p> <pre><code>using System.Windows.Forms; using Demo.Model; namespace Demo.Controls { public partial class OrderView : UserControl { private Order _order; public Order Order { get { return _order; } set { _order = value; UpdateView(); } } private void UpdateView() { if (_order == null) return; IdLBL.Text = string.Format("ID: {0}", _order.Id); DateLBL.Text = string.Format("Date: {0}", _order.Date); ItemsDGV.DataSource = _order.Items; } public OrderView() { InitializeComponent(); } } } </code></pre> <p>Then in main form I'm adding instances of that control to flowLayoutPanel (for every order for specific person):</p> <pre><code>private void RefreshView() { flowLayoutPanel1.Controls.Clear(); foreach (Order order in _persons[_currentPerson].Orders) { flowLayoutPanel1.Controls.Add(new OrderView {Order = order}); } } </code></pre> <p>With above data my application looks like so: <img src="https://i.stack.imgur.com/OZZrz.png" alt="enter image description here"></p> <p>I need to be able to add/edit every item of every order. Adding seems to be quite easy-I'll create new form, user will input details and then I'll do DB call to add that item.</p> <p>My questions are:<br> <strong>How can I auto refresh view after add/edit item?</strong> Can I somehow bind my control to that list to every time I update it my view updates. I need to be able to add items and orders to person. <strong>What would be the easiest way of doing that?</strong></p> <p><strong>Is this kind of display correct? Can I improve it? If yes then how?</strong></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload