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

Grid - data binding to hierarchical data set

3 Answers 249 Views
Grid
This is a migrated thread and some comments may be shown as answers.
PAYDAY
Top achievements
Rank 1
PAYDAY asked on 09 Jan 2009, 09:28 PM
Hi all,

I'd like to display a hierarchical grid - Master/Detail (or parent/child if you will).  I've created a dataset that contains the parent and child records. My dataset only returns columns I want to display...in other words, I don't want to "hard-code" columns in the HTML markup  or in the code behind. I also DON"T want to use any object datasource controls (ie: SQLDataSourceControl). Simply, I want to bind and display whatever data is in the dataset directly to the grid control.

Can you please provide me with the grid HTML and code that will allow me to bind this dataset to a grid?

        public static DataSet GetHierarchicalDataSet()
        {
                DataTable parentTable = GetParentData();  //retrieves parent data from database
                parentTable.TableName = "Parent";
                DataTable childTable = GetChildData();  //retrieves child data from database
                childTable.TableName = "Child"

                DataSet ds = new DataSet();
                ds.Tables.Add(parentTable.Copy());
                ds.Tables.Add(childTable.Copy());

                DataColumn parentColumn = ds.Tables["Parent"].Columns["ParentID"];
                DataColumn childColumn = ds.Tables["Child"].Columns["ParentID"];
                DataRelation relationship = new DataRelation("ParentChild", parentColumn, childColumn);
                ds.Relations.Add(relationship);

                return ds;
        } 





3 Answers, 1 is accepted

Sort by
0
Sebastian
Telerik team
answered on 12 Jan 2009, 02:23 PM
Hi Kevin,

The functionality you depicted (auto-generated hierarchy from dataset with tables and relations set) is not supported by our web grid. I will log your request to our product backlog to be considered for the future versions of the control. I also updated your Telerik points for the feedback.

Your current options are either to define declarative relations for the grid hierarchy or generate the detail table content on demand intercepting the DetailTableDataBind event of the grid. Review the following online demos for further details on both approaches:

http://demos.telerik.com/aspnet-ajax/Grid/Examples/Hierarchy/DeclarativeRelations/DefaultCS.aspx
http://demos.telerik.com/aspnet-ajax/Grid/Examples/Programming/DetailTableDataBind/DefaultCS.aspx

Best regards,
Sebastian
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
PAYDAY
Top achievements
Rank 1
answered on 12 Jan 2009, 03:55 PM
Okay - so I've used the DetailTableDataBind example to create the sample below. This seems to work fine and until I'm able to bind directly to the hierarchical data set, this will suffice. Thanks for your help.

HTML:
<telerik:RadGrid ID="HeirarchialGrid" runat="server" Width="400px" OnNeedDataSource="HeirarchialGrid_NeedDataSource"
    OnDetailTableDataBind="HeirarchialGrid_DetailTableDataBind">
    <MasterTableView AutoGenerateColumns="true">
        <DetailTables>
            <telerik:GridTableView Name="ChildGrid" AutoGenerateColumns="true">
            </telerik:GridTableView>
        </DetailTables>
    </MasterTableView>
</telerik:RadGrid>

CODE:
    protected void HeirarchialGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        HeirarchialGrid.DataSource = ParentEntity.GetData();
        HeirarchialGrid.MasterTableView.DataKeyNames = new string[] { "ParentEntityID" };
    }

    protected void HeirarchialGrid_DetailTableDataBind(object source, GridDetailTableDataBindEventArgs e)
    {
        GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem;
        switch (e.DetailTableView.Name)
        {
            case "ChildGrid":
                {
                    string parentEntityID= dataItem.GetDataKeyValue("ParentEntityID").ToString();
                    e.DetailTableView.DataSource = ChildEntity.GetData(parentEntityID);
                    break;
                }
        }
    }


0
K
Top achievements
Rank 1
answered on 17 Feb 2011, 08:29 PM
Hi,

It looks like the backlog for this feature made it into production.  I'm using version 2010.3.1312.35 code very similiar to PayDay's GetHierarchicalDataSet() function.  

Autogenerate hierarchy
Tags
Grid
Asked by
PAYDAY
Top achievements
Rank 1
Answers by
Sebastian
Telerik team
PAYDAY
Top achievements
Rank 1
K
Top achievements
Rank 1
Share this question
or