Building a hierarchy is a very simple task. Data is added to a grid via Grid.Rows.Add(object dataObject) function where the data object can be any object from string[] class to any object of arbitrary classes. This method returns Row-type object that is associated with the inserted data object. The resulting row also has Row.Add() method that adds data on the next hierarchical level returning new Row object. Please see below an example of building the simplest application hierarchy.

 Copy imageCopy
 public void PopulateGrid(Grid grid)
{
    //Create some products
    Product product1 = new Product();
    Product product2 = new Product();
    Product product3 = new Product();

    //Add a customer to the grid. He has bought product1 and product2
    Row row1 = grid.Rows.Add(new Customer());
    //Add products as children rows to the already added client
    row1.Add(product1);
    row1.Add(product2);

    //Add other customer to the grid. He has bought product2 and product3
    Row row2 = grid.Rows.Add(new Customer());
    //Add products as children rows to the already added client
    row2.Add(product2);
    row2.Add(product3);
}

Back to .Net Grid HowTo topics