I have 3 objects:
public class Person { public int Id { get; set; } public string Name { get; set; } public string Surname { get; set; } public List<Order> Orders { get; set; } public Person() { Orders= new List<Order>(); } } public class Order { public int Id { get; set; } public string Description { get; set; } public string Date { get; set; } public List<Item> Items { get; set; } public Order() { Items= new List<Item>(); } } public class Item { public int Id { get; set; } public string ProductName { get; set; } public int Number { get; set; } }
As you can see, each person can have several orders, and each order can have several elements.
In my data, the applications that come from the database look like this:
private List<Person> _persons; _persons = new List<Person> { new Person { Id = 1, Name = "John", Surname = "Smith", Orders = new List<Order> { new Order { Id = 1, Description = "First Order", Date = "2013-03-07", Items = new List<Item> { 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<Item> { 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<Order> { new Order { Id = 1, Description = "Adams order", Date = "2013-03-07", Items = new List<Item> { 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<Item> { new Item {Id = 1, Number = 2, ProductName = "Pen"}, new Item {Id = 2, Number = 1, ProductName = "Pencil"} } } } } };
I created a user control with two labels and a datagridview as follows:
below is my 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(); } } }
Then in the main form, I add instances of this flowLayoutPanel control (for each order for a specific person):
private void RefreshView() { flowLayoutPanel1.Controls.Clear(); foreach (Order order in _persons[_currentPerson].Orders) { flowLayoutPanel1.Controls.Add(new OrderView {Order = order}); } }
With the above data, my application looks like this: 
I need to be able to add / edit each item of each order. Adding seems quite simple - I will create a new form, the user will enter the details, and then I will make a call to the database to add this element.
My questions:
How can I automatically update the view after adding / editing an item? Can I somehow attach my control to this list every time I update it to update my view. I need to be able to add items and orders to a person. What would be the easiest way to do this?
Is this view displayed correctly? Can i improve it? If so, how?