Hierarchy Grid with list object defined

7 posts, 0 answers
  1. samuel
    samuel avatar
    12 posts
    Member since:
    Sep 2008

    Posted 17 Aug 2010 Link to this post

    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
  2. Veli
    Admin
    Veli avatar
    2002 posts

    Posted 20 Aug 2010 Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. samuel
    samuel avatar
    12 posts
    Member since:
    Sep 2008

    Posted 20 Aug 2010 Link to this post

    Thank you very much for your help
  5. JJ
    JJ avatar
    147 posts
    Member since:
    May 2010

    Posted 28 Jul 2011 Link to this post

    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!

     

  6. Marin
    Admin
    Marin avatar
    1043 posts

    Posted 03 Aug 2011 Link to this post

    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.

  7. JJ
    JJ avatar
    147 posts
    Member since:
    May 2010

    Posted 05 Aug 2011 Link to this post

    Thanks!

    What is GetItemByIndex?     radgrid function? can you give me more detail? 
  8. Veli
    Admin
    Veli avatar
    2002 posts

    Posted 10 Aug 2011 Link to this post

    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.

Back to Top
UI for ASP.NET Ajax is Ready for VS 2017