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

Hierarchy Grid with list object defined

6 Answers 230 Views
Grid
This is a migrated thread and some comments may be shown as answers.
samuel
Top achievements
Rank 1
samuel asked on 17 Aug 2010, 09:06 AM

I use C # with Visual Studio 2010.
I use an class Order and I have a property <OrderDetail> List. And I would use a grid to binding my class and to use the hierarchy to display also OrderDetail. But I do not know how. Can you explain to me?


Here are my classes simplified
public class Order
{
    public Order()
    {
    }
  
    public int Id {get;set;}
    public List<OrderDetail> ListOrderDetail {get;set;}
}
public class OrderDetail
{
    public OrderDetail()
    {
    }
    public string Article{get;set;}
    public int Qte {get;set;}
    public double Price {get;set;}
    public double PriceHT {get {return Qte*Price;}}
}

Thank you in advance

6 Answers, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 20 Aug 2010, 07:47 AM
Hello samuel,

There are a couple of approaches you can take for setting up a hierarchical RadGrid with the given object structure. You can use RadGrid's NeedDataSource event to bind RadGrid to the Orders collection you have. You can then use the DetailTableDataBind event to bind the detail tables to their parent Order item's child collection (ListOrderDetail) in your case:

protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    List<Order> orders = new List<Order>();   
    //populate orders here
    RadGrid1.DataSource = orders;
}
 
protected void RadGrid1_DetailTableDataBind(object sende, GridDetailTableDataBindEventArgs e)
{
    e.DetailTableView.DataSource = ((Order)e.DetailTableView.ParentItem.DataItem).ListOrderDetail;
}

The markup for the above example is:

<telerik:RadGrid ID="RadGrid1" runat="server" Width="800px"
    OnNeedDataSource="RadGrid1_NeedDataSource"
    OnDetailTableDataBind="RadGrid1_DetailTableDataBind">
    <MasterTableView HierarchyLoadMode="ServerBind">
        <DetailTables>
            <telerik:GridTableView>
            </telerik:GridTableView>
        </DetailTables>
    </MasterTableView>
</telerik:RadGrid>

Note the HierarchyLoadMode of the MasterTableView. It should be set to either ServerBind or Client. This setting is required, so that all your detail tables are databound when the parent items bind and you have access to the DataItem (which is the Order object in our case) of the parent item from the nested  table:

e.DetailTableView.ParentItem.DataItem

If you want to use LoadOnDemand hierarchy binding, you cannot use the e.DetailTableView.ParentItem.DataItem property. You will have to retrieve the Order object of the parent item separately from your data source to bind to its child ListOrderDetail collection.

Attaching a test page to demonstrate the above approach.

Greetings,
Veli
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
samuel
Top achievements
Rank 1
answered on 20 Aug 2010, 07:53 AM
Thank you very much for your help
0
JJ
Top achievements
Rank 1
answered on 28 Jul 2011, 04:33 PM
I used your sample to work on my grid to bind to the collections.  When I set detail table allow to paging and set page size to 2 using your attached sample,  The detail table paging fails .

 

<telerik:gridtableview Name="Detail" PageSize="2">

 

 

</telerik:gridtableview>

((Order)e.DetailTableView.ParentItem.DataItem).ListOrderDetail becomes null when detail table deos the paging.

Can you please help?

Thanks!

 

0
Marin
Telerik team
answered on 03 Aug 2011, 12:13 PM
Hi Jj,

 The DataItem property of the Parent item is not persisted when paging in the details table you can use the ItemIndex property instead to extract the correct item from the datasource and bind the detail table:

var item = GetItemByIndex(e.DetailTableView.ParentItem.ItemIndex); //method that gets the item from the the datasource by its index
e.DetailTableView.DataSource = item.ListOrderDetail;

Hope this helps.Regards,
Marin
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.

0
JJ
Top achievements
Rank 1
answered on 05 Aug 2011, 06:40 PM
Thanks!

What is GetItemByIndex?     radgrid function? can you give me more detail? 
0
Veli
Telerik team
answered on 10 Aug 2011, 11:01 AM
What Marin meant is that you need to find your data object from the data source that corresponds to the parent data item of the detail table. Once you retrieve the data object, you can bind the detail table to this object's child collection. You can find the parent data object either by the sequential ItemIndex of the parent GridDataItem instance (retrieved by e.DetailTable.ParentItem.ItemIndex), or by a data key value you have specified (using the e.DetailTable.ParentItem.GetDataKeyValue() method).

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
samuel
Top achievements
Rank 1
Answers by
Veli
Telerik team
samuel
Top achievements
Rank 1
JJ
Top achievements
Rank 1
Marin
Telerik team
Share this question
or