This is a migrated thread and some comments may be shown as answers.

Binding hierarchical grid with complex object

2 Answers 86 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mark Huber
Top achievements
Rank 1
Mark Huber asked on 07 Dec 2010, 10:30 PM
I have what I hope is a simple question, but I can't seem to find an example. I have the following object structure (for example):

public class Invoice
{
    public int InvoiceId { get; set; }
    public string CustomerName { get; set; }
    public double Total { get; set; }
 
    public List<InvoiceLineItem> LineItems { get; set;}
}
 
public class InvoiceLineItem
{
    public string Description { get; set; }
    public int Quantity { get; set; }
    public decimal Rate { get; set; }
    public decimal Total { get { return Quantity * Rate; } }
}

I want to bind a list of Invoice to this hierarchical grid using HierarchyLoadMode="Client" , preferably declaratively, and without hooking DetailTableDataBind (I have already solved it this way, but it seems there should be a much better way). Is there a way to express the relationship? All the data is preloaded and ready to be bound.

Current Solution:
protected void RadGridInvoices_DetailTableDataBind(object sender, GridDetailTableDataBindEventArgs e)
{
    var invoices= e.DetailTableView.DataSource as IEnumerable<Invoice>;
 
    if (invoices != null)
    {
        string invoiceId =
e.DetailTableView.ParentItem.GetDataKeyValue("InvoiceId").ToString();
 
        var invoice = invoices.First(inv => inv.InvoiceId == invoiceId);
             
        e.DetailTableView.DataSource = invoice.LineItems;
    }
}


Thanks,
Mark Huber

2 Answers, 1 is accepted

Sort by
0
Mark Huber
Top achievements
Rank 1
answered on 09 Dec 2010, 03:45 PM
bump.
0
Veli
Telerik team
answered on 11 Dec 2010, 08:30 AM
Hi Mark,

This is the only recommended approach if you are using custom object collections. You need to use DetailTableDataBind. The other alternative is to use ObjectDataSource controls and bind your detail table to one (through DataSourceID) that will fetch all InvoiceLineItem objects for a specified InvoiceId. You can refer to RadGrid's Hierarchy with Declarative DataSource Controls demo for an example on how to setup hierarchy in this way.

Veli
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Tags
Grid
Asked by
Mark Huber
Top achievements
Rank 1
Answers by
Mark Huber
Top achievements
Rank 1
Veli
Telerik team
Share this question
or