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

RadGrid Hierarchy Sub Details table not showed in rad grid.

2 Answers 49 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kannadasan
Top achievements
Rank 1
Kannadasan asked on 07 Jan 2011, 12:03 PM
Dear Team,

I Have attached  Sceen Shot for my problems.Give correct solutions or please progrmatical how to create this..give sample..code..
I give that Code Below..in detail bind..

    protected void rdReport_DetailTableDataBind(object source, Telerik.Web.UI.GridDetailTableDataBindEventArgs e)

        {

 

            GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem;

 

 

            #region Child

            string columnname = Session["ColumnNam"].ToString();

            string ReportId = Session["ReportId"].ToString();

            string mapping = Session["MappingField"].ToString();

            string Column = columnname.TrimEnd(',');

            string tablename = TableName(ReportId);

            #endregion

 

            #region SubChild

            string ChildColumnName = Session["ChildColumnNam"].ToString();

            string ChildReportId = Session["ChildReport_Id"].ToString();

            string ChildMapping = Session["ChildMapping_Field"].ToString();

            string ChildColumn = ChildColumnName.TrimEnd(',');

            string Childtablename = TableName(ChildReportId);

            #endregion

 

switch (e.DetailTableView.Name)

            {

                case "Child":

                    {

                        string DataKeyNames = dataItem.GetDataKeyValue(mapping).ToString();

                        string query = "select " + Column + " from [" + tablename + "] WHERE " + mapping + " ='" + DataKeyNames + "'";

                        e.DetailTableView.DataSource = GetDataTable(query);

                        break;

                    }

                case "SubChild":

                    {

                        string Query = "select " + ChildColumn + " from [" + Childtablename + "] WHERE " + ChildMapping + " = '" + dataItem[ChildMapping].Text + "'";

                        e.DetailTableView.DataSource = GetDataTable(Query);

                        break;

                    }

            }

}





With Regards.
S.Kannadasan

2 Answers, 1 is accepted

Sort by
0
Alexanders
Top achievements
Rank 1
answered on 16 Oct 2013, 03:15 PM
Hello, S.Kannadasan

I encountered the same problem, did you found solution for this issue?

Regards

0
Princy
Top achievements
Rank 2
answered on 17 Oct 2013, 05:33 AM
Hi ,

Please try the sample code snippet that shows programmatic Hierarchy Grid.Please try and let me know if any concerns.PLease share your code if this doesn't help.

ASPX:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

C#:
RadGrid RadGrid1;  
   protected void Page_Init(object source, System.EventArgs e)
   {
       DefineGridStructure();
   }
   private void DefineGridStructure()
   {
       RadGrid1 = new RadGrid();    
       RadGrid1.MasterTableView.DataKeyNames = new string[] { "CustomerID" };
       RadGrid1.Skin = "Default";
       RadGrid1.Width = Unit.Percentage(100);
       RadGrid1.PageSize = 15;
       RadGrid1.AllowPaging = true;
       RadGrid1.AutoGenerateColumns = false;
       RadGrid1.ViewStateMode = System.Web.UI.ViewStateMode.Enabled;
       RadGrid1.NeedDataSource += new GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);
       RadGrid1.DetailTableDataBind+=new GridDetailTableDataBindEventHandler(RadGrid1_DetailTableDataBind);
       
       RadGrid1.MasterTableView.EnableNoRecordsTemplate = true;
       //Add columns
       GridBoundColumn boundColumn;
       boundColumn = new GridBoundColumn();
       boundColumn.DataField = "CustomerID";
       boundColumn.HeaderText = "CustomerID";
       RadGrid1.MasterTableView.Columns.Add(boundColumn);
       boundColumn = new GridBoundColumn();
       boundColumn.DataField = "ContactName";
       boundColumn.HeaderText = "Contact Name";
       RadGrid1.MasterTableView.Columns.Add(boundColumn);
 
       //Detail table - Orders (II in hierarchy level)
       GridTableView tableViewOrders = new GridTableView(RadGrid1);    
       tableViewOrders.Name = "Orders";     
       tableViewOrders.DataKeyNames = new string[] { "OrderID" };
       GridRelationFields relationFields = new GridRelationFields();
       relationFields.MasterKeyField = "CustomerID";
       relationFields.DetailKeyField = "CustomerID";
       tableViewOrders.ParentTableRelation.Add(relationFields);
       RadGrid1.MasterTableView.DetailTables.Add(tableViewOrders);
       //Add columns
       boundColumn = new GridBoundColumn();
       boundColumn.DataField = "OrderID";
       boundColumn.HeaderText = "OrderID";
       tableViewOrders.Columns.Add(boundColumn);
       boundColumn = new GridBoundColumn();
       boundColumn.DataField = "OrderDate";
       boundColumn.HeaderText = "Date Ordered";
       tableViewOrders.Columns.Add(boundColumn);
       //Detail table Order-Details (III in hierarchy level)
       GridTableView tableViewOrderDetails = new GridTableView(RadGrid1);    
       tableViewOrderDetails.Name = "OrderDetails";
       tableViewOrderDetails.DataKeyNames = new string[] { "OrderID" };
       GridRelationFields relationFields2 = new GridRelationFields();
       relationFields2.MasterKeyField = "OrderID";
       relationFields2.DetailKeyField = "OrderID";
       tableViewOrderDetails.ParentTableRelation.Add(relationFields2);
       tableViewOrders.DetailTables.Add(tableViewOrderDetails);
       boundColumn = new GridBoundColumn();
       boundColumn.DataField = "UnitPrice";
       boundColumn.HeaderText = "Unit Price";
       tableViewOrderDetails.Columns.Add(boundColumn);
       boundColumn = new GridBoundColumn();
       boundColumn.DataField = "Quantity";
       boundColumn.HeaderText = "Quantity";
       tableViewOrderDetails.Columns.Add(boundColumn);
       //Add the RadGrid instance to the controls
       this.PlaceHolder1.Controls.Add(RadGrid1);
   }
 
   protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
   {
       if (!e.IsFromDetailTable)
       {
           RadGrid1.DataSource = GetDataTable("SELECT * FROM Customers");
       }
   }
 
   protected void RadGrid1_DetailTableDataBind(object source, Telerik.Web.UI.GridDetailTableDataBindEventArgs e)
   {
       GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem;
       switch (e.DetailTableView.Name)
       {
           case "Orders":
               {
                   string CustomerID = dataItem.GetDataKeyValue("CustomerID").ToString();
                   e.DetailTableView.DataSource = GetDataTable("SELECT * FROM Orders WHERE CustomerID = '" + CustomerID + "'");
                   break;
               }
 
           case "OrderDetails":
               {
                   string OrderID = dataItem.GetDataKeyValue("OrderID").ToString();
                   e.DetailTableView.DataSource = GetDataTable("SELECT * FROM [Order Details] WHERE OrderID = " + OrderID);
                   break;
               }
       }
   }
 
   public DataTable GetDataTable(string query)
   {
       String ConnString = ConfigurationManager.ConnectionStrings["Northwind_newConnectionString3"].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;
   }

Thanks,
Princy
Tags
Grid
Asked by
Kannadasan
Top achievements
Rank 1
Answers by
Alexanders
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or