JSchwartz
JSchwartz

Reputation: 2724

How to display a collection inside a collection?

I have the following classes:

public class Order
{
public string OrderName { get; set; }
public List<Parts> PartsList { get; set; }
}

public class Parts
{
public string PartName { get; set; }
public double PartQuantity { get; set; }
}

In my code I create a list of Order objects

List<Order> myOrders;

I would like to display all of this to the user somehow, like using a stack panel of elements where the first is a TextBox to display OrderName and the second is a Datagrid to display the list of Parts?

Honestly I am trying many different things (I have no requirements on what type of controls to use) but I can never get the PartsList to show correctly (either I get nothing OR I get "Collection" show to the user.

The goal would be to see something like this:

Order1    Part1    7
          Part2    12
Order2    Part1    100
          Part2    1
          Part3    58

Or whatever, headers can be whatever, honestly I am open to many suggestions.

Any help would be much appreciated.

Upvotes: 0

Views: 83

Answers (1)

Erik
Erik

Reputation: 12858

Would a TreeView work? It would look something like this:

- Order 1
  * Part A (100)
  * Part B (200)
+ Order 2
+ Order 3
- Order 4
  * Part C (300)
  * Part D (400)
  * Part E (500)
+ Order 5
...

You could do something like this:

void AddOrderToTree(Order order)
{
  var orderNode = treeView.Nodes.Add(order);

  foreach(var part in order.Parts)
    orderNode.Nodes.Add(part);
}

But yes, as pointed out, I would use properties like so:

public class Part
{
  public virtual string Name { get; set; }
  public virtual double Quantity { get; set; }

  public override string ToString()
  {
    return string.Format("{0}: {1}", this.Name, this.Quantity.ToString());
  }
}

public class Order
{
  public virtual string Name { get; set; }
  public virtual List<Part> Parts { get; set; }

  public override string ToString()
  { 
    return this.Name;
  }
}

Upvotes: 2

Related Questions