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

[Solved] Hierarchy and Object data source

3 Answers 259 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kyler
Top achievements
Rank 1
Kyler asked on 13 Mar 2008, 07:27 PM

I have a List(Of Customer) and part of the Customer object is a List(Of Order).  I programmatically bind the Customers, using RadGrid1.Datasource = CustomerList.  I want a DetailTable under each Customer row with all Orders.  The examples I've seen use table data sources.  I would expect something like...

Protected Sub RadGrid1_ItemDataBound(ByVal sender As ObjectByVal e As   
Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound  
If e.Item.ItemType = GridItemType.Item Or e.Item.ItemType =   
GridItemType.AlternatingItem Or e.Item.ItemType = GridItemType.SelectedItem Then   
Dim myCustomer As Customer = e.Item.DataItem  
If myCustomer.hasOrders Then 
'The following is pseudo-code  
e.Item.DetailTable.DataSource = myCustomer.Orders  
e.Item.DataBind()  
End If 
End If 
End Sub 

But that doesn't seem to be how to crack this nut.  Any help is appreciated.

3 Answers, 1 is accepted

Sort by
0
Sebastian
Telerik team
answered on 17 Mar 2008, 03:04 PM
Hello Kyler,

RadGrid exposes DetailTableDataBind event which can be hooked to populate the nested tables in hierarchical grid with data:

http://www.telerik.com/DEMOS/ASPNET/Prometheus/Grid/Examples/Programming/DetailTableDataBind/DefaultCS.aspx
http://www.telerik.com/help/radcontrols/prometheus/?grdHierarchicalDataBindingUsingDetailTableDataBind.html

Please revise your code to conform to the concepts explained in the resources and you should be done.

Best,
Stephen
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Kyler
Top achievements
Rank 1
answered on 14 Apr 2008, 05:19 PM
Thanks for your reply.  I noticed that when I include a DetailTable, and assign the MasterTableView a DataSource in the NeedDataSource event, the DetailTable defaults to the same datasource.  So if my datasource has 4 Persons, I get 4 top level rows, and each of those rows has the same 4 children rows.  To fix this, I tried using the DetailTableDataBind event shown below.  But if the Person has no DirectReports, setting that Person's DetailTable DataSource to "Nothing" has no effect.  In fact, if I check that Person's DetailTable DataSource just after setting it to "Nothing" I see that it is still the original 4 Persons.  Make sense?
 

Private Sub RadGrid1_DetailTableDataBind(ByVal source As Object, ByVal e As GridDetailTableDataBindEventArgs) Handles RadGrid1.DetailTableDataBind

Dim dataItem As GridDataItem = CType(e.DetailTableView.ParentItem, GridDataItem)

Dim myPerson As Person = dataItem.DataItem

If myPerson.hasDirectReports Then

e.DetailTableView.DataSource = myPerson.DirectReports

Else

'this next line doesn't work

e.DetailTableView.DataSource =

Nothing

End If

End Sub

0
Sebastian
Telerik team
answered on 15 Apr 2008, 06:31 AM
Hello Kyler,

Basically, the data source for a detail table should not match this for the master table since these are separate elements in the grid. Can you please double-check that the code which assigns the data source for the main table in the grid is enclosed within the following conditional check (as in this online demo linked previously):

      Private Sub RadGrid1_NeedDataSource(ByVal source As ObjectByVal e As GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource  
            If Not e.IsFromDetailTable Then 
                //set the data source for the master table here  
            End If 
        End Sub 

Additionally, if you want to assign an empty data source for a detail table when certain condition is met, use the following syntax inside the DetailTableDataBind handler:

<DetailTableInstance>.DataSource = new Object(){}

Best regards,
Stephen
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
Grid
Asked by
Kyler
Top achievements
Rank 1
Answers by
Sebastian
Telerik team
Kyler
Top achievements
Rank 1
Share this question
or