RadPanelBar in NestedGridView of RadGrid

2 posts, 0 answers
  1. Richard
    Richard avatar
    28 posts
    Member since:
    Jun 2010

    Posted 22 Jan 2013 Link to this post

    Hi,

    I have a RadGrid on my page that contains a NestedViewTemplate containing a RadPanelBar.  I have my HierarchyLoadMode set to ServerOnDemand.  What I would like to do is when the user clicks to expand a row, I want the RadPanelBar to list a set of sub-records of row I am clicking on.  I need the RadPanelBar to contain some aspet controls, so I am using the ContentTemplate.  What I have done is, in the DetailTableDataBind function, I am querying a database for my subrecords and binding the results to the RadPanelBar for that particular GridDataItem being passed into the function.  For each row of my sub-table, I am dynamically creating my ContentTemplate from a class I created based on another post on the Telerik site.  Below is the code for this.  In additional, in my Page_Load function I am looping through the RadGrid and creating the custom template again.  Everything I have done appears to work fine with one exception.  When I first open a detail row, the RadPanelBar in that row does not have the collapse/expand ability in the header.  If I open up a second row, the first row's collapse/expand then appears and everything looks great.  But of course the second row's collapse/expand isn't visible.  So I'm always one off from this working without issue :)  I've tried using the OnInit function to do what I am doing in the Page_Load, but whenever I do anything with the RadGrid in this function, opening the nestedview causes the entire grid to disappear from the page (almost looks like it lost it's data bindings).  I have a hunch that the 'OnDetailTableDataBind' is part of my issue, I am guessing I should be doing what I am doing there somewhere else since technically I don't need to even set the e.DetailTableView.DataSource for this to work.  Does anyone have any ideas where I might be off?  My two functions are below. 

    Thanks for any suggestions!
    Richard
    protected void OnDetailTableDataBind(object sender, GridDetailTableDataBindEventArgs e)
            {
                GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem;
                int customerid = Convert.ToInt32(dataItem.GetDataKeyValue("customerid"));
                RadPanelBar panelBar = (RadPanelBar)dataItem.ChildItem.FindControl("pbCustomerDetails");
                using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["LocalConnectionString"].ConnectionString))
                {
                    using (SqlCommand myCommand = new SqlCommand("", conn))
                    {
                        myCommand.CommandText = "Select DateOfBirth, WorkAddress from CustomerDetail where Active=1 and CustomerID=@CustomerID ";
                        myCommand.Parameters.Add("@CustomerID", SqlDbType.Int).Value = customerid;
                        using (SqlDataAdapter adapter = new SqlDataAdapter(myCommand))
                        {
                            DataTable table = new DataTable();
                            conn.Open();
                            adapter.Fill(table);
                            conn.Close();
                             
                            panelBar.DataSource = table;
                            panelBar.DataBind();
                            for (int i = 0; i < panelBar.Items.Count; i++)
                            {
                                panelBar.Items[i].ContentTemplate = new CustomerTemplate();
                                panelBar.Items[i].ContentTemplate.InstantiateIn(panelBar.Items[i]);
                                panelBar.Items[i].DataBind();
                                panelBar.Items[i].Expanded = true;
                            }
                             
                            panelBar.DataBind();
                            e.DetailTableView.DataSource = table;
                        }
                                 
                    }
                }
            }

    protected void Page_Load(object sender, EventArgs e)
            {
                foreach (GridDataItem item in CustomerGrid.Items)
                {
                    if (item.Expanded)
                    {
                        RadPanelBar panelBar = (RadPanelBar)item.ChildItem.FindControl("pbCustomerDetails");
                        for (int i = 0; i < panelBar.Items.Count; i++)
                        {
                            panelBar.Items[i].ContentTemplate = new CustomerTemplate();
                            panelBar.Items[i].ContentTemplate.InstantiateIn(panelBar.Items[i]);
                            panelBar.Items[i].DataBind();
                        }
                        panelBar.DataBind();
                    }
     
                }           
            }
  2. Boyan Dimitrov
    Admin
    Boyan Dimitrov avatar
    1746 posts

    Posted 25 Jan 2013 Link to this post

    Hello,

    Our recommended approach for applying and instantiating templates would be using the OnInit event. Here you can find a help article explaining in details that functionality and sample source code that might be helpful.
    Additionally I would recommend you to find the RadPanelBar control in the current GridItem  OnPageLoad from the provided code snippet.
    //code behind
    protected void Page_Load(object sender, EventArgs e)
        {
            foreach (GridDataItem item in RadGrid1.Items)
            {
                if (item.Expanded)
                {
                    RadPanelBar panelBar = (RadPanelBar)item.ChildItem.NestedViewCell.FindControl("RadPanelBar1");
    .............
                }
      
            }
        }

    Kind regards,
    Boyan Dimitrov
    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top