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

Programmatic Hierarchy Binding

2 Answers 83 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Josh
Top achievements
Rank 1
Josh asked on 03 Mar 2015, 07:24 PM
Hi,

I am trying to create a hierarchical grid as found in this demo. I am not sure that what I am trying to do is possible due to the way my data is structured. I am pulling
data from multiple Oracle tables and storing it in a single SQL Server table. This table is basically a view of the Oracle data and as such I am obviously losing any relations that existed in the original data. So I guess my question is...is it possible to create a hierarchical grid from a single database table?

Using the before referenced demo, I receive the following error:

There was a problem extracting DataKeyValues from the DataSource. Please ensure that DataKeyNames are specified correctly and all fields specified exist in the
DataSource.

I have the following fields in my SQL Server table:
proj_id, proj_short_name, wbs_id, wbs_name, task_id, task_code, task_name, short_name, proc_id, proc_name, proc_wt, complete_pct

wbs_id and proc_id represent the composite primary_key

The top level of the RadGrid should show wbs_name and short_name
The 2nd level should show task_id, task_name
The 3rd level should show proc_name, proc_wt and complete_pct

Here is my markup and code:

ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="practiceTemplate.aspx.cs" Inherits="ProgramStatusManagementSystemWebApp.primavera.practiceTemplate" %>
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
 
<!DOCTYPE html>
 
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server"></telerik:RadScriptManager>
 
        <telerik:RadSkinManager ID="RadSkinManager1" runat="server" ShowChooser="true"></telerik:RadSkinManager>
 
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
            <AjaxSettings>
                <telerik:AjaxSetting AjaxControlID="RadGrid1">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
            </AjaxSettings>
        </telerik:RadAjaxManager>
 
        <telerik:RadGrid ID="RadGrid1" runat="server" ShowStatusBar="true" AutoGenerateColumns="False"
 
            PageSize="7" AllowSorting="True" AllowMultiRowSelection="False" AllowPaging="True"
 
            OnDetailTableDataBind="RadGrid1_DetailTableDataBind" OnNeedDataSource="RadGrid1_NeedDataSource"
 
            OnPreRender="RadGrid1_PreRender">
 
            <PagerStyle Mode="NumericPages"></PagerStyle>
 
            <MasterTableView DataKeyNames="wbs_id" AllowMultiColumnSorting="True">
 
                <DetailTables>
 
                    <telerik:GridTableView DataKeyNames="wbs_id" Name="Activities" Width="100%">
 
                        <DetailTables>
 
                            <telerik:GridTableView DataKeyNames="task_id" Name="StepDetails" Width="100%">
 
                                <Columns>
                                     <telerik:GridBoundColumn SortExpression="proc_id" HeaderText="Step ID" HeaderButtonType="TextButton"
 
                                        DataField="proc_id" UniqueName="proc_id">
 
                                    </telerik:GridBoundColumn>
 
                                    <telerik:GridBoundColumn SortExpression="proc_name" HeaderText="Step Name" HeaderButtonType="TextButton"
 
                                        DataField="proc_name" UniqueName="proc_name">
 
                                    </telerik:GridBoundColumn>
 
                                    <telerik:GridBoundColumn SortExpression="proc_wt" HeaderText="Step Weight" HeaderButtonType="TextButton"
 
                                        DataField="proc_wt" UniqueName="proc_wt">
 
                                    </telerik:GridBoundColumn>
 
                                    <telerik:GridBoundColumn SortExpression="complete_pct" HeaderText="Percent Complete" HeaderButtonType="TextButton"
 
                                        DataField="complete_pct">
 
                                    </telerik:GridBoundColumn>
 
                                </Columns>
 
                            </telerik:GridTableView>
 
                        </DetailTables>
 
                        <Columns>
 
                            <telerik:GridBoundColumn SortExpression="task_code" HeaderText="Activity ID" HeaderButtonType="TextButton"
 
                                DataField="task_code" UniqueName="task_code">
 
                            </telerik:GridBoundColumn>
 
                            <telerik:GridBoundColumn SortExpression="task_name" HeaderText="Activity Name" HeaderButtonType="TextButton"
 
                                DataField="task_name" UniqueName="task_name" DataFormatString="{0:D}">
 
                            </telerik:GridBoundColumn>                           
 
                        </Columns>
 
                    </telerik:GridTableView>
 
                </DetailTables>
 
                <Columns>
 
                    <telerik:GridBoundColumn SortExpression="wbs_name" HeaderText="WBS Name" HeaderButtonType="TextButton"
 
                        DataField="wbs_name" UniqueName="wbs_name">
 
                    </telerik:GridBoundColumn>
 
                    <telerik:GridBoundColumn SortExpression="short_name" HeaderText="CAM" HeaderButtonType="TextButton"
 
                        DataField="short_name" UniqueName="short_name">
 
                    </telerik:GridBoundColumn>                  
 
                </Columns>
 
            </MasterTableView>
 
        </telerik:RadGrid>
 </div>
    </form>
</body>
</html>

C#:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
 
namespace ProgramStatusManagementSystemWebApp.primavera
{
    public partial class practiceTemplate : System.Web.UI.Page
    {
         
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
 
        protected void RadGrid1_DetailTableDataBind(object sender, Telerik.Web.UI.GridDetailTableDataBindEventArgs e)
        {
            GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem;
            switch (e.DetailTableView.Name)
            {
                case "Activities":
                    {
                        string wbsID = dataItem.GetDataKeyValue("wbs_id").ToString();
                        //string rowuid = dataItem.GetDataKeyValue("rowuid").ToString();
                        e.DetailTableView.DataSource = GetDataTable("SELECT task_code, task_name FROM PrimaveraStatusTemplate3 WHERE wbs_id = " + wbsID + "");
                        //e.DetailTableView.DataSource = GetDataTable("SELECT task_code, task_name FROM PrimaveraStatusTemplate3 WHERE rowuid = " + rowuid + "");
                        break;
                    }
 
                case "StepDetails":
                    {
                        string taskID = dataItem.GetDataKeyValue("task_id").ToString();
                        e.DetailTableView.DataSource = GetDataTable("SELECT proc_name, proc_wt, complete_pct FROM PrimaveraStatusTemplate3 WHERE task_id = " + taskID + "");
                        break;
                    }
            }
        }
 
        protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            if (!e.IsFromDetailTable)
            {
                RadGrid1.DataSource = GetDataTable("SELECT * FROM PrimaveraStatusTemplate3");
            }
        }
 
        protected void RadGrid1_PreRender(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                RadGrid1.MasterTableView.Items[0].Expanded = true;
 
                RadGrid1.MasterTableView.Items[0].ChildItem.NestedTableViews[0].Items[0].Expanded = true;
            }
        }
 
        public DataTable GetDataTable(string query)
        {
            String ConnString = ConfigurationManager.ConnectionStrings["PSMSystemConnectionString"].ConnectionString;
 
            SqlConnection conn = new SqlConnection(ConnString);
 
            SqlDataAdapter adapter = new SqlDataAdapter();
 
            adapter.SelectCommand = new SqlCommand(query, conn);
 
 
 
            DataTable myDataTable = new DataTable();
 
 
 
            conn.Open();
 
            try
            {
 
                adapter.Fill(myDataTable);
 
            }
 
            finally
            {
 
                conn.Close();
 
            }
 
 
 
            return myDataTable;
        }
    }
}

The error is being thrown on RadGrid1_PreRender.

Thanks in advance,

Josh




2 Answers, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 06 Mar 2015, 01:51 PM
Hi Josh,

Based on the provided description it seems that you would like to display self-referencing data in RadGrid. Although this can be achieved it is not recommended to use RadGrid in this scenario.

I would recommend using RadTreeList control in your project. It is optimized to display self-referencing data and would work better for you. Check out the following online demo that illustrates how RadTreeList works.



Regards,
Viktor Tachev
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Josh
Top achievements
Rank 1
answered on 06 Mar 2015, 08:10 PM
Viktor,

Thank you for the reply. I wasn't aware or familiar with the RadTreeList control. I do think it will work for what I am trying to do. Thank you for the suggestion.

Josh
Tags
Grid
Asked by
Josh
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Josh
Top achievements
Rank 1
Share this question
or