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

Hierarchy with Templates example

4 Answers 194 Views
Grid
This is a migrated thread and some comments may be shown as answers.
andieje
Top achievements
Rank 1
andieje asked on 21 May 2009, 07:57 PM
Hi
I would like to modify the hierarchy with templates demo for the radgrid but I am having a few problems understandinging it.

Here's the link

Firstly in the NestedViewTemplate there is this label inside the one of the pageviews of the multipageview control
          <asp:Label ID="Label1" Font-Bold="true" Font-Italic="true" Text='<%# Eval("EmployeeID") %>'
                                    Visible="false" runat="server" />


I don't understand what is going on here: Text='<%# Eval("EmployeeID")

Is the data from the current row in the master table also being bound to the NestedViewTemplate? I suppose it must be otherwise this label wouldn't have the correct value. Can you explain to me how/why the data is being bound to the NestedViewTemplate.

I have a second question. In the demo there is a grid inside the nested view template which is bound declaratively using an SQL data source. I would like to have a grid inside the template but i want to bind it programmatically and i don't know how. Is this possible and where do i do it? I presume in some data binding event for the nestedviewtemplate I have to find the grid from the controls collection of the nestedviewtemplate. But i don't know - I'm guessing. Any help would be much appreciated.

Many thanks
andrea

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 22 May 2009, 05:43 AM
Hello Andrea,

In the demo, it is based on the hidden Label's text (EmployeeID) that the nested grid is populated. Check out the select parameter of the SqlDataSource to which the nested grid is bound. So it is based on the EmployeeId of the main grid that the OrdersGrid is populated
<SelectParameters> 
      <asp:ControlParameter ControlID="Label1" PropertyName="Text" Type="String" Name="EmployeeID" /> 
 </SelectParameters>

To populate the nested grid dynamically, you can access the grid in the PreRender event and then populate it:
c#:
 protected void RadGrid1_PreRender(object sender, EventArgs e) 
    {     
       foreach (GridDataItem item in RadGrid1.MasterTableView.Items) 
        { 
            if (item.Expanded) 
            { 
                foreach (GridNestedViewItem item1 in RadGrid1.MasterTableView.GetItems(GridItemType.NestedView)) 
                { 
                    RadGrid grid = (RadGrid)item1.FindControl("OrdersGrid"); 
                    grid.DataSource = "  "// set the datasource here 
                } 
            } 
         } 
     } 

Thanks
Princy.

0
andieje
Top achievements
Rank 1
answered on 22 May 2009, 08:18 PM
Hi
Thanks for your reply. However it didn't explain what i wanted to know and i think that's because I didn't word my question properly. I can see that the nested grid is bing populated from the swl data source and that the select parameter for this data source is linked to the label containing the employee ID. My question is how/why is the NestedViewTemplate being populated with data from the parent row/item in the first place. I am assuming that it must be built into the rad grid somehow that when data is bound to the grid that each row in the data source is bound not only to a row/item in the rad grid but also to a nestedviewtemplate for the row/item if there is one. If this is the case it is not made explicit anywhere in the documentation.

I will try out your solution for programmatic binding to nested grid now.

Thanks
0
andieje
Top achievements
Rank 1
answered on 22 May 2009, 09:19 PM
Hi

Thanks-you for your reply. The code for programmatic binding does indeed work. However the code finds every single NestedViewItem in the grid and hence binds every nested grid in every NestedViewItem. This makes the page incredibly slow. In fact it times out for me most of the time. I tried to alter the code so that the nested grid is only populated if it is found in the NestedViewItem of an expanded grid item.

The best I could come up with is this below. This works and is much faster

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        Dim item As GridDataItem
        Dim s As String = ""
        For Each item In RadGrid1.MasterTableView.Items
            If item.Expanded Then


                Dim nestedItem As GridNestedViewItem
                For Each nestedItem In RadGrid1.MasterTableView.GetItems(GridItemType.NestedView)

                    Dim l As Label = nestedItem.FindControl("Label1")
                    If String.Compare(l.Text, item.GetDataKeyValue("CustomerID")) = 0 Then
                        Dim grid As RadGrid = nestedItem.FindControl("OrdersGrid")
                        Dim sql As String = "select * from orders where customerID = '" & item.GetDataKeyValue("CustomerID") & "'"
                        grid.DataSource = GetDataReader(sql)
                        grid.DataBind()
                    End If
                Next
            End If
        Next
End Sub

Before this i tried things like comparing the nestedviewitem.parentItem.id to the id of the expanded item but this didn't work as the expanded item id is an empty string for every row. I tried comparing the value of label1 to the data key value of the expanded data item in the hope that only the nestedviewitem of expanded items are populated so only these would have a value for label1 and so only these would pass the string comparison. However iit would seem all nestedview items are populated, even if their parent item is collapsed so in this case every row passed the string comparison.

If you can show me a better way than this (or perhaps this is the only way) it would be much appreciated

thanks
0
Sebastian
Telerik team
answered on 26 May 2009, 08:14 AM
Hello andieje,

A possible method to optimize the performance and bind merely the nested view for the expanded item would be to wrap the nested view content in asp Panel and dynamically show/hide it based on the expanded state of the parent item. This is presented on the same online demo of the product (see the ItemCreated, ItemCommand and PreRender handlers for details).

Best regards,
Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Grid
Asked by
andieje
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
andieje
Top achievements
Rank 1
Sebastian
Telerik team
Share this question
or