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

Setting DataSource at runtime in NestedTable

3 Answers 153 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Paul
Top achievements
Rank 1
Paul asked on 19 Dec 2008, 08:39 PM
Hello,

I have a hierachial RadGrid and would like to be able to set the DataSource for the nested tables at runtime on the server based on criteria in the parent grid.  Currently, it looks a little bit like this:

<telerik:RadGrid ID="grid1" Skin="Default" runat="server" Visible="False" AutoGenerateColumns="False" 
    ShowHeader="False" Width="400px" OnItemDataBound="grid1_ItemDataBound" AllowMultiRowSelection="True" 
    OnRowDrop="grid1_RowDrop" OnDeleteCommand="grid1_DeleteCommand" OnPreRender="grid1_PreRender"
    <MasterTableView DataKeyNames="AnswerID"
    <DetailTables>  
        <telerik:GridTableView Name="grid2">         
            <Columns> 
            <telerik:GridBoundColumn AllowFiltering="False" AllowSorting="False" DataField="AnswerLabel" 
                ReadOnly="True" Reorderable="False" Resizable="False" ShowSortIcon="False" UniqueName="colAnswerLabel"
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField="QuestionID" UniqueName="colQuestionID" Visible="False"
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField="AnswerID" UniqueName="colAnswerID" Visible="False"
            </telerik:GridBoundColumn> 
            </Columns> 
            <RowIndicatorColumn> 
            <HeaderStyle Width="20px" /> 
            </RowIndicatorColumn> 
            <ExpandCollapseColumn> 
            <HeaderStyle Width="20px" /> 
            </ExpandCollapseColumn> 
        </telerik:GridTableView>  
    </DetailTables> 
    <Columns> 
        <telerik:GridBoundColumn AllowFiltering="False" AllowSorting="False" DataField="AnswerLabel" 
        ReadOnly="True" Reorderable="False" Resizable="False" ShowSortIcon="False" UniqueName="colAnswerLabel"
        </telerik:GridBoundColumn> 
        <telerik:GridBoundColumn DataField="QuestionID" UniqueName="colQuestionID" Visible="False"
        </telerik:GridBoundColumn> 
        <telerik:GridBoundColumn DataField="AnswerID" UniqueName="colAnswerID" Visible="False"
        </telerik:GridBoundColumn> 
    </Columns> 
    <RowIndicatorColumn> 
        <HeaderStyle Width="20px" /> 
    </RowIndicatorColumn> 
    <ExpandCollapseColumn> 
        <HeaderStyle Width="20px" /> 
    </ExpandCollapseColumn> 
    </MasterTableView> 
    <ClientSettings AllowRowsDragDrop="True"
    <Selecting AllowRowSelect="True" /> 
    </ClientSettings> 
</telerik:RadGrid> 
 
 


During the course of the app, I can easily bind to grid1, but how can I bind DataTables to grid2 that are not in a relational database relationship to grid1? Is there a way to bind one or two of the nested grids at runtime based on information contained in the corresponding row in grid1 and provided by the user?

3 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 22 Dec 2008, 06:33 AM
Hello Paul,

You can try out the following code to set the datasource for the detail table based on a condition in the parent row.
cs:
 protected void RadGrid1_PreRender(object sender, EventArgs e) 
    { 
        foreach (GridDataItem item in RadGrid1.MasterTableView.Items) 
        { 
            GridTableView nestedView = (GridTableView)item.ChildItem.NestedTableViews[0]; 
            if (item["parentrowColumnUniqueName"].Text == "Text") 
                nestedView.DataSourceID = "SqlDataSource1"
            else if (item["parentrowColumnUniqueName"].Text != "Text") 
                nestedView.DataSourceID = ""
        } 
    } 

Thanks
Princy.


0
Paul
Top achievements
Rank 1
answered on 06 Jan 2009, 04:43 PM
Princy, I forgot to thank you for this reply. It was very helpful. In particular, this line was the challenging part:

GridTableView nestedView = (GridTableView)item.ChildItem.NestedTableViews[0]; 

Thanks again.

0
Princy
Top achievements
Rank 2
answered on 07 Jan 2009, 04:14 AM
Hi Paul,

You can refer to the following help document, to learn more on how to access the nested table view(detail tables) of the each item in the grid's master table.
Traversing detail tables/items in Telerik RadGrid

Happy New Year!
Princy.
Tags
Grid
Asked by
Paul
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Paul
Top achievements
Rank 1
Share this question
or