The grid provides numerous data binding features and supports creation of any hierarchies with simple tools. A new feature – bulding a hierarchy with DataRelation – was added in version 2.8.1. This feature is based on conditional data binding used in the grid.

When a grid is bound to a data table, the table may have relations with other tables. DataRow objects in the data table have DataRow.GetChildRows() method that returns children collection based on DataRelation. Therefore, to build a hierarchy it is necessary to tell the grid that adding rows are linked with children rows via DataRelation. This should be done at the time of data binding. The example below shows how to do it with simple tools using conditional data binding:

C# Copy imageCopy
public static DataSet CreateDataSet()
{
    // create a DataSet with one table, two columns
    DataSet dataSet = new DataSet();

    // create the 'Customers' table
    DataTable table = new DataTable("Customers");
    dataSet.Tables.Add(table);
    table.Columns.Add("customerId", typeof(int)).AutoIncrement = true;
    table.Columns.Add("name", typeof(string));
    table.PrimaryKey = new DataColumn[] { table.Columns["customerId"] };

    // create the 'Orders' table
    table = new DataTable("Orders");
    dataSet.Tables.Add(table);
    table.Columns.Add("orderId", typeof(int)).AutoIncrement = true;
    table.Columns.Add("customerId", typeof(int));
    table.Columns.Add("amount", typeof(double));
    table.PrimaryKey = new DataColumn[] { table.Columns["orderId"] };

    // create a data relation
    dataSet.Relations.Add("myrelation", 
                                  dataSet.Tables["Customers"].Columns["customerId"], 
                                  dataSet.Tables["Orders"].Columns["customerId"]);

    Random r = new Random();

    // populate the 'Customers' table
    int orderId = 1;
    const int customerCount = 10;
    const int ordersPerCustomer = 5;
    for (int customerId = 1; customerId <= customerCount; customerId++)
    {
        // add a new customer
        dataSet.Tables["Customers"].Rows.Add(new object[] { customerId, string.Format("customer{0}", customerId) });
    }

    // populate the 'Orders' table
    for (int order = 1; order <= customerCount * ordersPerCustomer; order++)
    {
        // add an order
        dataSet.Tables["Orders"].Rows.Add(new object[] { orderId++, 1 + r.Next() % customerCount, (10 + r.Next() % 10) });
    }

    return dataSet;
}

//Initialize the grid:
Header header1 = new Header();
header1.Add(new Column("customerId", "customerId", 60, ContentAlignment.MiddleCenter));
header1.Add(new Column("name", "Customer name"));
header1.StretchMode = ColumnStretchMode.All;
grid.Headers.Add(header1);

Header header2 = new Header();
header2.Add(new Column("orderId", "orderId", 60, ContentAlignment.MiddleCenter));
header2.Add(new Column("amount", "amount"));
header2.StretchMode = ColumnStretchMode.All;
header2.Visible = false;

grid.Headers.Add(header2);

//Add an event handler to handle data relation at adding time
grid.RowAdding += delegate(object sender, GridRowAddingEventArgs e)
{
    //The object is adding on the root level
    if (e.ParentRow == null)
    {
        e.InsertionType = InsertionType.AsDataRelation;
        e.DataObject = "myrelation";    
    }
};

//Bind .Net Grid to the "Customers" table
grid.DataSource = dataSet.Tables["Customers"];

Back to .Net Grid HowTo topics