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

[Solved] Populating Hierarchical Data using code behind

6 Answers 218 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tadhg Lydon
Top achievements
Rank 1
Tadhg Lydon asked on 18 May 2010, 10:35 PM
I have a datagrid with hierarchical data that I want to populate not using the SqlDataSource reference but from the code behind cs.

I can populate the root data table but not sure how I go about populating the sub tables of the datagrid.

From aspx:

<telerik:RadGrid ID="rgrid_Data" runat="server" AllowPaging="True" AutoGenerateColumns="False" ShowGroupPanel="True" OnItemDataBound="rgrid_Data_ItemDataBound">                    
                    <MasterTableView AutoGenerateColumns="false">                                      
                    <Columns>
                        <telerik:GridBoundColumn HeaderText="Month" DataField="item" UniqueName="item"></telerik:GridBoundColumn>
                    </Columns>                    
                    <DetailTables>
                    <telerik:GridTableView AutoGenerateColumns="false" DataKeyNames="item" >
                    <ParentTableRelation>
                       <telerik:GridRelationFields DetailKeyField="StartMonth" MasterKeyField="item" />
                    </ParentTableRelation>
                    <Columns>
                                <telerik:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName= "DeleteColumn"></telerik:GridButtonColumn>
                                <telerik:GridButtonColumn CommandName="Update" Text="Update" UniqueName= "UpdateColumn"></telerik:GridButtonColumn>
                                <telerik:GridBoundColumn HeaderText="ID" DataField="Id" UniqueName= "Id"></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn HeaderText="Start Date" DataField="StartDate" UniqueName= "StartDate"></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn HeaderText="Expiry Date" DataField="ExpiryDate" UniqueName= "ExpiryDate"></telerik:GridBoundColumn>                           
                            </Columns>
                    </telerik:GridTableView>                    
                    </DetailTables>                     
                    </MasterTableView>
                    
                </telerik:RadGrid>

So to populate the root dataset I've been using

this.rgrid_Data.DataSource = dataSet;
this.rgrid_Data.DataBind();

and in trying to populate the sub sections I've been going down the path

this.rgrid_Data.MasterTableView.DetailTables[0].DataSource = subDataSet;
this.rgrid_Data.MasterTableView.DetailTables[0].DataBind(); 

Any help, much appreciated.

6 Answers, 1 is accepted

Sort by
0
Pavlina
Telerik team
answered on 19 May 2010, 08:20 AM
Hello Tadhg,

Please go through the online resources below, which demonstrate how to dynamically bind a hierarchical Grid:
Programmatic Binding
Hierarchical data-binding using DetailTableDataBind event

Kind regards,
Pavlina
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
Tadhg Lydon
Top achievements
Rank 1
answered on 19 May 2010, 10:58 AM
Thank you. That was exactly what I was looking for :)
0
Tim
Top achievements
Rank 1
answered on 30 Apr 2013, 10:03 PM
The "OnDetailTableDataBind" event does not happen when you are using a NestedViewTemplate.  How can I bind the child grids programatically using a Nested template?
0
Shinu
Top achievements
Rank 2
answered on 02 May 2013, 04:53 AM
Hi,

You can populate the inner grid in its NeedDataSource event as shown below.
C#:
protected void RadGrid2_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
   {
       SqlConnection con1 = new SqlConnection(WebConfigurationManager.ConnectionStrings["NorthwindConnectionString3"].ConnectionString);
  RadGrid innergrid=(RadGrid)sender;
        GridNestedViewItem nesteditem = (GridNestedViewItem)innergrid.NamingContainer;
        string dataKeyValue = Convert.ToString(((GridDataItem)(nesteditem.ParentItem)).GetDataKeyValue("CustomerID"));
   SqlCommand cmd = new SqlCommand("SELECT top 5 * FROM [Orders] where CustomerID=@CustomerID", con1);
   SqlDataAdapter ad = new SqlDataAdapter(cmd);
       DataSet ds = new DataSet();
  ad.Fill(ds);
       innergrid.DataSource = ds;
   }

Thanks,
Shinu
0
Tim
Top achievements
Rank 1
answered on 02 May 2013, 08:45 PM
I can try it again, but that event never fires when a Parent/Master row is expanded.  It only fires that event when a Master row is collapsed.  My code was based on your online demo of the Nested template.  Maybe it has something to do with the fact that the asp:Panel that wraps everything is invisible until expanded?
0
Eyup
Telerik team
answered on 03 May 2013, 11:56 AM
Hi Tim,

I have prepared a sample RadGrid web site to demonstrate how you can use the NeedDataSource event of the inner grids. Please check out the attached application and let me know if it helps you.

All the best,
Eyup
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Tadhg Lydon
Top achievements
Rank 1
Answers by
Pavlina
Telerik team
Tadhg Lydon
Top achievements
Rank 1
Tim
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Eyup
Telerik team
Share this question
or